diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/document/createattribute/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/document/createattribute/index.html')
-rw-r--r-- | files/zh-cn/web/api/document/createattribute/index.html | 90 |
1 files changed, 90 insertions, 0 deletions
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 +--- +<div>{{ ApiRef("DOM") }}</div> + +<p><code><strong>Document.createAttribute()</strong></code> 方法创建并返回一个新的属性节点。这个对象创建一个实现了 {{domxref("Attr")}} 接口的节点。这个方式下DOM不限制节点能够添加的属性种类。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>attribute</em> = document.createAttribute(name)</pre> + +<h3 id="参数">参数</h3> + +<ul> + <li><code>name</code> ,属性的属性名。</li> +</ul> + +<h3 id="返回值">返回值</h3> + +<p>一个 {{domxref("Attr")}} 节点。</p> + +<h3 id="异常">异常</h3> + +<ul> + <li><code>INVALID_CHARACTER_ERR</code> 如果参数含有非法的XML属性字符。</li> +</ul> + +<h2 id="例子">例子</h2> + +<pre class="brush:js">var node = document.getElementById("div1"); +var a = document.createAttribute("my_attrib"); +a.value = "newVal"; +node.setAttributeNode(a); +console.log(node.getAttribute("my_attrib")); // "newVal" +</pre> + +<h2 id="备注">备注</h2> + +<p>该方法的返回值是一个类型为 <code>Attr</code> 的节点。你可以通过为该节点的 <code>nodeValue</code> 属性赋值来设置该属性节点的属性值,也可以使用常用的 <a href="/zh-CN/docs/Web/API/Element/setAttribute">setAttribute()</a> 方法来替代该方法.</p> + +<h2 id="规范">规范</h2> + +<table class="spectable standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('DOM WHATWG','#dom-document-createattribute','Document.createAttribute()')}}</td> + <td>{{Spec2("DOM WHATWG")}}</td> + <td>大写字符的精确表现。</td> + </tr> + <tr> + <td>{{SpecName('DOM3 Core','core.html#ID-1084891198','Document.createAttribute()')}}</td> + <td>{{Spec2('DOM3 Core')}}</td> + <td>无变更。</td> + </tr> + <tr> + <td>{{SpecName('DOM2 Core','core.html#ID-1084891198','Document.createAttribute()')}}</td> + <td>{{Spec2('DOM2 Core')}}</td> + <td>无变更。</td> + </tr> + <tr> + <td>{{SpecName('DOM1','level-one-core.html#ID-1084891198','Document.createAttribute()')}}</td> + <td>{{Spec2('DOM1')}}</td> + <td>初始定义。</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div class="hidden">这个页面的兼容性表是从格式化数据生成的,如果你想贡献数据,请check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>修改后给我们发送pull请求。</div> + +<p>{{Compat("api.Document.createAttribute")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{domxref("Document.createElement()")}}</li> +</ul> |