HTML DOM setAttributeNode() 方法
JavaScript基础 2022-06-08 11:30:56小码哥的IT人生shichen
HTML DOM setAttributeNode() 方法
实例
设置 header 元素的 class 属性节点:
var atr=document.createAttribute("class");
atr.nodeValue="democlass";
document.getElementsByTagName("H1")[0].setAttributeNode(atr);
设置属性节点之前:
Hello World
设置属性节点之后:
Hello World
完整实例:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.democlass{
color:red;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<p id="demo">点击按钮来设置上面这个标题的 class 属性节点。</p>
<button onclick="myFunction()">试一下</button>
<script>
function myFunction()
{
var atr=document.createAttribute("class");
atr.nodeValue="democlass";
var h=document.getElementsByTagName("H1")[0];
h.setAttributeNode(atr);
}
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
定义和用法
setAttributeNode() 方法向元素中添加指定的属性节点。
如果这个指定的属性已存在,则此方法会替换它。
浏览器支持
IE | Firefox | Chrome | Safari | Opera |
---|---|---|---|---|
所有主流浏览器均支持 setAttributeNode() 方法。
语法
element.setAttributeNode(attributenode)
参数
参数 | 类型 | 描述 |
---|---|---|
attributenode | Attr 对象 | 必需。您希望添加的属性节点。 |
返回值
类型 | 描述 |
---|---|
Attr 对象。 | 被替换的属性节点,如果有,否则返回 null。 |
技术细节
DOM 版本 | Core Level 1 Element Object |