文档:createAttribute() 方法
Document.createAttribute() 方法创建一个新的属性节点并返回它。创建的对象是一个实现 Attr 接口的节点。DOM 不强制规定通过此方法可以向特定元素添加何种类型的属性。
注意: 参数中的字符串会被转换为小写。
语法
js
createAttribute(name)
参数
name-
包含属性名称的字符串。
返回值
一个 Attr 节点。
异常
InvalidCharacterErrorDOMException-
如果
name值不是有效的 XML 名称,则抛出此错误;例如,名称以数字、连字符或句点开头,或者包含除字母数字字符、下划线、连字符或句点以外的字符。
示例
js
const node = document.getElementById("div1");
const a = document.createAttribute("my_attrib");
a.value = "newVal";
node.setAttributeNode(a);
console.log(node.getAttribute("my_attrib")); // "newVal"
规范
| 规范 |
|---|
| DOM # dom-document-createattribute |
浏览器兼容性
加载中…