代码示例:(标识:jsck_document_createattribute)
<!DOCTYPE html>
<html>
<body>

<h1>Document 对象</h1>

<h2>createAttribute() 和 setAttributeNode() 方法</h2>

<p><a id="myAnchor">访问 sou-xun.com</a></p>

<p>请点击“添加”,为上面的链接添加一个 href 属性。</p>

<button onclick="myFunction()">添加</button>

<script>
function myFunction() {
// 创建 href 属性:
const attr = document.createAttribute("href");

// 设置 href 属性的值:
attr.value = "https://www.sou-xun.com";

// 为元素添加 href 属性:
document.getElementById("myAnchor").setAttributeNode(attr);
}
</script>

</body>
</html>
运行结果: