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/element/toggleattribute | |
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/element/toggleattribute')
-rw-r--r-- | files/zh-cn/web/api/element/toggleattribute/index.html | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/element/toggleattribute/index.html b/files/zh-cn/web/api/element/toggleattribute/index.html new file mode 100644 index 0000000000..c98a1882ef --- /dev/null +++ b/files/zh-cn/web/api/element/toggleattribute/index.html @@ -0,0 +1,113 @@ +--- +title: Element.toggleAttribute() +slug: Web/API/Element/toggleAttribute +tags: + - API + - Element + - 元素 + - 参考 +translation_of: Web/API/Element/toggleAttribute +--- +<div>{{APIRef("DOM")}}</div> + +<p>{{domxref("Element")}} 接口的 <code><strong>toggleAttribute()</strong></code> 方法切换给定元素的某个布尔值属性的状态(如果属性不存在则添加属性,属性存在则移除属性)。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><em>Element</em>.toggleAttribute(<em>name</em> [, <em>force</em>]); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>name</code></dt> + <dd>A {{domxref("DOMString")}} specifying the name of the attribute to be toggled. The attribute name is automatically converted to all lower-case when <code>toggleAttribute()</code> is called on an HTML element in an HTML document.</dd> + <dt><code>force</code> {{optional_inline}}</dt> + <dd>A boolean value to determine whether the attribute should be added or removed, no matter whether the attribute is present or not at the moment.</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p><code>true</code> if attribute <strong><code>name</code></strong> is eventually present, and <code>false</code> otherwise.</p> + +<h3 id="异常">异常</h3> + +<dl> + <dt><code>InvalidCharacterError</code></dt> + <dd>The specified attribute <code>name</code> contains one or more characters which are not valid in attribute names.</dd> +</dl> + +<h2 id="例子">例子</h2> + +<p>在下面的例子中,<code>toggleAttribute()</code> 被用于切换 {{HTMLElement("input")}} 的 <code>readonly</code> 属性。</p> + +<h3 id="HTML">HTML</h3> + +<pre class="brush: html notranslate"><input value="text"> +<button>toggleAttribute("readonly")</button></pre> + +<h3 id="JavaScript">JavaScript</h3> + +<pre class="brush:js notranslate">var button = document.querySelector("button"); +var input = document.querySelector("input"); + +button.addEventListener("click", function(){ + input.toggleAttribute("readonly"); +}); +</pre> + +<h3 id="结果">结果</h3> + +<p>{{ EmbedLiveSample('Example', '300', '50') }}</p> + +<p>{{DOMAttributeMethods}}</p> + +<h2 id="Polyfill">Polyfill</h2> + +<div class="standardNoteBlock"> +<p><strong>译者注</strong>:下面代码中的 <code>void 0</code> 即 <code>undefined</code>。</p> +</div> + +<pre class="brush: js notranslate">if (!Element.prototype.toggleAttribute) { + Element.prototype.toggleAttribute = function(name, force) { + if(force !== void 0) force = !!force + + if (this.getAttribute(name) !== null) { + if (force) return true; + + this.removeAttribute(name); + return false; + } else { + if (force === false) return false; + + this.setAttribute(name, ""); + return true; + } + }; +} +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('DOM WHATWG', '#dom-element-toggleattribute', 'Element.toggleAttribute')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.Element.toggleAttribute")}}</p> |