@@ -87,10 +87,10 @@ document.body.sayHi(); // Hello, I'm BODY
87
87
88
88
当然。所有特性都可以通过使用以下方法进行访问:
89
89
90
- - ` elem.hasAttribute(name) ` — 检查特性是否存在。
91
- - ` elem.getAttribute(name) ` — 获取这个特性值。
92
- - ` elem.setAttribute(name, value) ` — 设置这个特性值。
93
- - ` elem.removeAttribute(name) ` — 移除这个特性。
90
+ - ` elem.hasAttribute(name) ` —— 检查特性是否存在。
91
+ - ` elem.getAttribute(name) ` —— 获取这个特性值。
92
+ - ` elem.setAttribute(name, value) ` —— 设置这个特性值。
93
+ - ` elem.removeAttribute(name) ` —— 移除这个特性。
94
94
95
95
这些方法操作的实际上是 HTML 中的内容。
96
96
@@ -135,7 +135,7 @@ HTML 特性有以下几个特征:
135
135
136
136
请注意:
137
137
138
- 1 . ` getAttribute('About') ` — 这里的第一个字母是大写的,但是在 HTML 中,它们都是小写的。但这没有影响:特性的名称是大小写不敏感的。
138
+ 1 . ` getAttribute('About') ` —— 这里的第一个字母是大写的,但是在 HTML 中,它们都是小写的。但这没有影响:特性的名称是大小写不敏感的。
139
139
2 . 我们可以将任何东西赋值给特性,但是这些东西会变成字符串类型的。所以这里我们的值为 ` "123" ` 。
140
140
3 . 所有特性,包括我们设置的那个特性,在 ` outerHTML ` 中都是可见的。
141
141
4 . ` attributes ` 集合是可迭代对象,该对象将所有元素的特性(标准和非标准的)作为 ` name ` 和 ` value ` 属性存储在对象中。
@@ -362,8 +362,8 @@ div.setAttribute('order-state', 'canceled');
362
362
363
363
## 总结
364
364
365
- - 特性(attribute)— 写在 HTML 中的内容。
366
- - 属性(property)— DOM 对象中的内容。
365
+ - 特性(attribute)—— 写在 HTML 中的内容。
366
+ - 属性(property)—— DOM 对象中的内容。
367
367
368
368
简略的对比:
369
369
@@ -374,11 +374,11 @@ div.setAttribute('order-state', 'canceled');
374
374
375
375
操作特性的方法:
376
376
377
- - ` elem.hasAttribute(name) ` — 检查是否存在这个特性。
378
- - ` elem.getAttribute(name) ` — 获取这个特性值。
379
- - ` elem.setAttribute(name, value) ` — 设置这个特性值。
380
- - ` elem.removeAttribute(name) ` — 移除这个特性。
381
- - ` elem.attributes ` — 所有特性的集合。
377
+ - ` elem.hasAttribute(name) ` —— 检查是否存在这个特性。
378
+ - ` elem.getAttribute(name) ` —— 获取这个特性值。
379
+ - ` elem.setAttribute(name, value) ` —— 设置这个特性值。
380
+ - ` elem.removeAttribute(name) ` —— 移除这个特性。
381
+ - ` elem.attributes ` —— 所有特性的集合。
382
382
383
383
在大多数情况下,最好使用 DOM 属性。仅当 DOM 属性无法满足开发需求,并且我们真的需要特性时,才使用特性,例如:
384
384
0 commit comments