You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/reference-dom-elements.md
+29-29Lines changed: 29 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
id: dom-elements
3
-
title: DOM Elements
3
+
title: DOM 元素
4
4
layout: docs
5
5
category: Reference
6
6
permalink: docs/dom-elements.html
@@ -14,27 +14,27 @@ redirect_from:
14
14
- "tips/dangerously-set-inner-html.html"
15
15
---
16
16
17
-
React implements a browser-independent DOM system for performance and cross-browser compatibility. We took the opportunity to clean up a few rough edges in browser DOM implementations.
17
+
React 实现了一套独立于浏览器的 DOM 系统,兼顾了性能和跨浏览器的兼容性。我们借此机会完善了浏览器 DOM 实现的一些特殊情况。
18
18
19
-
In React, all DOM properties and attributes (including event handlers) should be camelCased. For example, the HTML attribute`tabindex`corresponds to the attribute `tabIndex` in React. The exception is `aria-*`and`data-*`attributes, which should be lowercased. For example, you can keep `aria-label`as`aria-label`.
19
+
在 React 中,所有的 DOM 特性和属性(包括事件处理)都应该是小驼峰命名的方式。例如,与 HTML 中的`tabindex`属性对应的 React 的属性是 `tabIndex`。例外的情况是 `aria-*`以及`data-*`属性,一律使用小写字母命名。比如, 你依然可以用 `aria-label`作为`aria-label`。
20
20
21
-
## Differences In Attributes {#differences-in-attributes}
21
+
## 属性差异 {#differences-in-attributes}
22
22
23
-
There are a number of attributes that work differently between React and HTML:
23
+
React 与 HTML 之间有很多属性存在差异:
24
24
25
25
### checked {#checked}
26
26
27
-
The `checked` attribute is supported by `<input>`components of type `checkbox`or`radio`. You can use it to set whether the component is checked. This is useful for building controlled components.`defaultChecked`is the uncontrolled equivalent, which sets whether the component is checked when it is first mounted.
27
+
当 `<input>`组件的 type 类型为 `checkbox`或`radio` 时,组件支持 `checked` 属性。你可以使用它来设置组件是否被选中。这对于构建受控组件(controlled components)很有帮助。而`defaultChecked`则是非受控组件的属性,用于设置组件首次挂载时是否被选中。
28
28
29
29
### className {#classname}
30
30
31
-
To specify a CSS class, use the `className`attribute. This applies to all regular DOM and SVG elements like `<div>`, `<a>`, and others.
31
+
`className`属性用于指定 CSS 的 class,此特性适用于所有常规 DOM 节点和 SVG 元素,如 `<div>`,`<a>` 及其它标签。
32
32
33
-
If you use React with Web Components (which is uncommon), use the `class` attribute instead.
33
+
如果你在 React 中使用 Web Components(这是一种不常见的使用方式),请使用 class 属性代替。
`dangerouslySetInnerHTML`is React's replacement for using`innerHTML`in the browser DOM. In general, setting HTML from code is risky because it's easy to inadvertently expose your users to a [cross-site scripting (XSS)](https://en.wikipedia.org/wiki/Cross-site_scripting) attack. So, you can set HTML directly from React, but you have to type out `dangerouslySetInnerHTML`and pass an object with a `__html`key, to remind yourself that it's dangerous. For example:
37
+
`dangerouslySetInnerHTML`是 React 为浏览器 DOM 提供`innerHTML`的替换方案。通常来讲,使用代码直接设置 HTML 存在风险,因为很容易无意中使用户暴露于[跨站脚本(XSS)](https://en.wikipedia.org/wiki/Cross-site_scripting)的攻击。因此,你可以直接在 React 中设置 HTML,但当你想设置 `dangerouslySetInnerHTML`时,需要向其传递包含 key 为 `__html`的对象,以此来警示你。例如:
38
38
39
39
```js
40
40
functioncreateMarkup() {
@@ -48,23 +48,23 @@ function MyComponent() {
48
48
49
49
### htmlFor {#htmlfor}
50
50
51
-
Since`for`is a reserved word in JavaScript, React elements use `htmlFor`instead.
The `onChange`event behaves as you would expect it to: whenever a form field is changed, this event is fired. We intentionally do not use the existing browser behavior because `onChange`is a misnomer for its behavior and React relies on this event to handle user input in real time.
The `selected` attribute is supported by `<option>`components. You can use it to set whether the component is selected. This is useful for building controlled components.
>Some examples in the documentation use `style` for convenience, but **using the `style`attribute as the primary means of styling elements is generally not recommended.** In most cases, [`className`](#classname)should be used to reference classes defined in an external CSS stylesheet. `style`is most often used in React applications to add dynamically-computed styles at render time. See also [FAQ: Styling and CSS](/docs/faq-styling.html).
The `style`attribute accepts a JavaScript object with camelCased properties rather than a CSS string. This is consistent with the DOM `style` JavaScript property, is more efficient, and prevents XSS security holes. For example:
67
+
`style`接受一个采用小驼峰命名属性的 JavaScript 对象,而不是 CSS 字符串。这与 DOM 中 `style`的 JavaScript 属性是一致的,同时会更高效的,且能预防跨站脚本(XSS)的安全漏洞。例如:
68
68
69
69
```js
70
70
constdivStyle= {
@@ -77,7 +77,7 @@ function HelloWorldComponent() {
77
77
}
78
78
```
79
79
80
-
Note that styles are not autoprefixed. To support older browsers, you need to supply corresponding style properties:
80
+
注意:样式不会自动补齐前缀。如需支持旧版浏览器,请手动补充对应的样式属性:
81
81
82
82
```js
83
83
constdivStyle= {
@@ -90,9 +90,9 @@ function ComponentWithTransition() {
90
90
}
91
91
```
92
92
93
-
Style keys are camelCased in order to be consistent with accessing the properties on DOM nodes from JS (e.g. `node.style.backgroundImage`). Vendor prefixes [other than `ms`](https://www.andismith.com/blogs/2012/02/modernizr-prefixed/) should begin with a capital letter. This is why `WebkitTransition`has an uppercase "W".
React will automatically append a "px" suffix to certain numeric inline style properties. If you want to use units other than "px", specify the value as a string with the desired unit. For example:
@@ -106,37 +106,37 @@ React will automatically append a "px" suffix to certain numeric inline style pr
106
106
</div>
107
107
```
108
108
109
-
Not all style properties are converted to pixel strings though. Certain ones remain unitless (eg`zoom`, `order`, `flex`). A complete list of unitless properties can be seen [here](https://github.com/facebook/react/blob/4131af3e4bf52f3a003537ec95a1655147c81270/src/renderers/dom/shared/CSSProperty.js#L15-L59).
Normally, there is a warning when an element with children is also marked as `contentEditable`, because it won't work. This attribute suppresses that warning. Don't use this unless you are building a library like [Draft.js](https://facebook.github.io/draft-js/)that manages `contentEditable`manually.
If you use server-side React rendering, normally there is a warning when the server and the client render different content. However, in some rare cases, it is very hard or impossible to guarantee an exact match. For example, timestamps are expected to differ on the server and on the client.
If you set `suppressHydrationWarning`to`true`, React will not warn you about mismatches in the attributes and the content of that element. It only works one level deep, and is intended to be used as an escape hatch. Don't overuse it. You can read more about hydration in the [`ReactDOM.hydrate()`documentation](/docs/react-dom.html#hydrate).
The `value` attribute is supported by `<input>`and`<textarea>`components. You can use it to set the value of the component. This is useful for building controlled components. `defaultValue`is the uncontrolled equivalent, which sets the value of the component when it is first mounted.
## All Supported HTML Attributes {#all-supported-html-attributes}
126
126
127
-
As of React 16, any standard [or custom](/blog/2017/09/08/dom-attributes-in-react-16.html) DOM attributes are fully supported.
127
+
在 React 16 中,任何标准的[或自定义的](/blog/2017/09/08/dom-attributes-in-react-16.html) DOM 属性都是完全支持的。
128
128
129
-
React has always provided a JavaScript-centric API to the DOM. Since React components often take both custom and DOM-related props, React uses the `camelCase` convention just like the DOM APIs:
129
+
React 为 DOM 提供了一套以 JavaScript 为中心的 API。由于 React 组件经常采用自定义或和 DOM 相关的 props 的关系,React 采用了`小驼峰命名`的方式,正如 DOM APIs 那样:
130
130
131
131
```js
132
132
<div tabIndex="-1"/>// Just like node.tabIndex DOM API
133
133
<div className="Button"/>// Just like node.className DOM API
134
134
<input readOnly={true} />// Just like node.readOnly DOM API
135
135
```
136
136
137
-
These props work similarly to the corresponding HTML attributes, with the exception of the special cases documented above.
137
+
除了上述文档提到的特殊拼写方式以外,这些 props 的用法与 HTML 的属性也极为类似。
138
138
139
-
Some of the DOM attributes supported by React include:
139
+
React 支持的 DOM 属性有:
140
140
141
141
```
142
142
accept acceptCharset accessKey action allowFullScreen alt async autoComplete
0 commit comments