Attr 对象
在 HTML DOM 中,Attr 对象表示 HTML 属性。
HTML 属性始终属于 HTML 元素。
NamedNodeMap 对象
在 HTML DOM 中,NamedNodeMap 对象表示元素属性节点的无序集合。
NamedNodeMap 中的节点可通过名称或索引(数字)来访问。
浏览器支持
IE | firefox | safari | opera | |
---|---|---|---|---|
true | true | true | true | true |
属性
1. name
返回属性的名称
浏览器支持
IE | firefox | safari | opera | |
---|---|---|---|---|
true | true | true | true | true |
DOM.attribute.name || attrObject.name
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>zsh</title>
</head>
<body>
<div id="div" name="My div"></div>
<script>
var attr = document.createAttribute('id');
attr.value = '123';
console.log(attr.name);
console.log(div.attributes[1].name);
</script>
</body>
</html>
2. value
设置或返回属性的值
浏览器支持
IE | firefox | safari | opera | |
---|---|---|---|---|
true | true | true | true | true |
attribute.value = 'something' || attrObject.value
<!doctype html>
<html lang="en">
<head>
<meta charset=