--- title: Element.setAttribute() slug: Web/API/Element/setAttribute tags: - API - DOM - NeedsSpecTable - 元素 - 参考 - 属性 - 方法 translation_of: Web/API/Element/setAttribute --- <div>{{APIRef("DOM")}}</div> <p><span class="seoSummary">设置指定元素上的某个属性值。如果属性已经存在,则更新该值;否则,使用指定的名称和值添加一个新的属性。</span></p> <p>要获取某个属性当前的值,可使用 {{domxref("Element.getAttribute", "getAttribute()")}};要删除某个属性,可使用 {{domxref("Element.removeAttribute", "removeAttribute()")}}。</p> <h2 id="语法">语法</h2> <pre class="syntaxbox notranslate"><em>element</em>.setAttribute(<em>name</em>, <em>value</em>); </pre> <h3 id="参数">参数</h3> <dl> <dt><code>name</code></dt> <dd>表示属性名称的字符串。A {{domxref("DOMString")}} specifying the name of the attribute whose value is to be set. The attribute name is automatically converted to all lower-case when <code>setAttribute()</code> is called on an HTML element in an HTML document.</dd> <dt><code>value</code></dt> <dd>属性的值/新值。A {{domxref("DOMString")}} 包含了分配给这个属性的值. 任何非字符串的值都会被自动转换为字符串.</dd> </dl> <p>当在 HTML 文档中的 HTML 元素上调用 <code>setAttribute()</code> 方法时,该方法会将其属性名称(attribute name)参数小写化。</p> <p>如果指定的属性已经存在,则其值变为传递的值。如果不存在,则创建指定的属性。</p> <p>尽管对于不存在的属性,<code><a href="/en-US/docs/DOM/element.getAttribute" title="DOM/element.getAttribute">getAttribute()</a></code> 返回 <code>null</code>,你还是应该使用 <code><a href="/en-US/docs/DOM/element.removeAttribute" title="DOM/element.removeAttribute">removeAttribute()</a></code> 代替 <code><em>elt</em>.setAttribute(<em>attr</em>, null)</code> 来删除属性。</p> <p>布尔属性(原文是Boolean attributes)只要出现在元素上就会被认为是 <code>true</code> ,无论它的值是什么; 一般来说, 你应该将 <code>value</code> 设置为空字符串 (<code>""</code>) 。(一些人使用这个属性的名称作为值; 这不会出现什么问题,但这是不规范的). See the {{anch("Example", "example")}} below for a practical demonstration.</p> <p>由于将指定的值转换为字符串,因此指定null不一定能达到您的期望。 而不是删除属性或将其值设置为{{jsxref("null")}},而是将属性的值设置为字符串“ null”。 如果要删除属性,请调用{{domxref("Element.removeAttribute”,“ removeAttribute()")}}}。</p> <h3 id="返回值">返回值</h3> <p>{{jsxref("undefined")}}</p> <h3 id="例外">例外</h3> <dl> <dt><code>无效字符错误</code></dt> <dd>指定的属性名称包含一个或多个在属性名称中无效的字符。</dd> </dl> <h2 id="例子">例子</h2> <p>在下面的例子中,<code>setAttribute()</code> 被用于设置 {{HTMLElement("button")}} 上的属性。</p> <h3 id="HTML">HTML</h3> <pre class="brush: html notranslate"><button>Hello World</button></pre> <h3 id="JavaScript">JavaScript</h3> <pre class="brush:js notranslate">var b = document.querySelector("button"); b.setAttribute("name", "helloButton"); b.setAttribute("disabled", ""); </pre> <p>这说明了两件事:</p> <ul> <li>上面对setAttribute()的第一次调用显示了将name属性的值更改为“ helloButton”。 您可以使用浏览器的页面检查器(Chrome,Edge,Firefox,Safari)查看此内容。</li> <li>要设置布尔属性的值(例如禁用),可以指定任何值。 建议使用空字符串或属性名称。 重要的是,如果根本不存在该属性,则不管其实际值如何,都将其值视为真实。 该属性的缺失表示其值是false。 通过将Disabled属性的值设置为空字符串(“”),我们将disable设置为true,这将导致按钮被禁用。</li> </ul> <p>{{ EmbedLiveSample('Example', '300', '50') }}</p> <p>{{DOMAttributeMethods}}</p> <h2 id="规范">规范</h2> <ul> <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-F68F082">DOM Level 2 Core: setAttribute</a> (introduced in <a class="external" href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#method-setAttribute">DOM Level 1 Core</a>)</li> <li><a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/#apis-in-html-documents">HTML5: APIs in HTML documents</a></li> </ul> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("api.Element.setAttribute")}}</p> <h3 id="Notes" name="Notes">Gecko 备注</h3> <p>使用 <code>setAttribute()</code> 修改某些属性值时,尤其是 XUL 中的 <code>value</code>,可能得不到期望结果。这是由于 <code>attribute</code> 指定的是默认值。要访问或修改当前值,应该使用 <code>property</code> 属性。例如,使用 <code>Element.value</code> 代替 <code>Element.setAttribute()</code>。</p>