<html>
<body>
<h1>Document 对象</h1>
<h2>createAttribute() 和 setAttributeNode() 方法</h2>
<p><a id="myAnchor">访问 W3school.com.cn</a></p>
<p>请点击“添加”,为上面的链接添加一个 href 属性。</p>
<button onclick="myFunction()">添加</button>
<script>
function myFunction() {
// 创建 href 属性:
const attr = document.createAttribute("href");
// 设置 href 属性的值:
attr.value = "https://www.w3school.com.cn";
// 为元素添加 href 属性:
document.getElementById("myAnchor").setAttributeNode(attr);
}
</script>
</body>
</html>