From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/document/createattribute/index.html | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 files/zh-cn/web/api/document/createattribute/index.html (limited to 'files/zh-cn/web/api/document/createattribute/index.html') diff --git a/files/zh-cn/web/api/document/createattribute/index.html b/files/zh-cn/web/api/document/createattribute/index.html new file mode 100644 index 0000000000..6b9e743ce9 --- /dev/null +++ b/files/zh-cn/web/api/document/createattribute/index.html @@ -0,0 +1,90 @@ +--- +title: document.createAttribute() +slug: Web/API/Document/createAttribute +tags: + - API + - DOM + - 参考 + - 方法 +translation_of: Web/API/Document/createAttribute +--- +
{{ ApiRef("DOM") }}
+ +

Document.createAttribute() 方法创建并返回一个新的属性节点。这个对象创建一个实现了 {{domxref("Attr")}} 接口的节点。这个方式下DOM不限制节点能够添加的属性种类。

+ +

语法

+ +
attribute = document.createAttribute(name)
+ +

参数

+ + + +

返回值

+ +

一个 {{domxref("Attr")}} 节点。

+ +

异常

+ + + +

例子

+ +
var node = document.getElementById("div1");
+var a = document.createAttribute("my_attrib");
+a.value = "newVal";
+node.setAttributeNode(a);
+console.log(node.getAttribute("my_attrib")); // "newVal"
+
+ +

备注

+ +

该方法的返回值是一个类型为 Attr 的节点。你可以通过为该节点的 nodeValue 属性赋值来设置该属性节点的属性值,也可以使用常用的 setAttribute() 方法来替代该方法.

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('DOM WHATWG','#dom-document-createattribute','Document.createAttribute()')}}{{Spec2("DOM WHATWG")}}大写字符的精确表现。
{{SpecName('DOM3 Core','core.html#ID-1084891198','Document.createAttribute()')}}{{Spec2('DOM3 Core')}}无变更。
{{SpecName('DOM2 Core','core.html#ID-1084891198','Document.createAttribute()')}}{{Spec2('DOM2 Core')}}无变更。
{{SpecName('DOM1','level-one-core.html#ID-1084891198','Document.createAttribute()')}}{{Spec2('DOM1')}}初始定义。
+ +

浏览器兼容性

+ + + +

{{Compat("api.Document.createAttribute")}}

+ +

参见

+ + -- cgit v1.2.3-54-g00ecf