{"id":9727,"date":"2020-12-06T00:29:37","date_gmt":"2020-12-05T16:29:37","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9727"},"modified":"2020-12-06T00:29:37","modified_gmt":"2020-12-05T16:29:37","slug":"js-toggleattribute-dom-api","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/12\/js-toggleattribute-dom-api\/","title":{"rendered":"\u539f\u6765DOM\u8fd8\u6709toggleAttribute\u8fd9\u6837\u7684JS API"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9727\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9727<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202012-toggle-attribute.png';<\/script><\/p>\n<h3>\u4e00\u3001\u6084\u65e0\u58f0\u606f\u65b0API<\/h3>\n<p>DOM\u5143\u7d20\u7684\u5c5e\u6027\u8bbe\u7f6eAPI\u5305\u62ec\uff1a<code>setAttribute(name, value)<\/code>\u3001<code>getAttribute(name)<\/code>\u3001<code>hasAttribute(name)<\/code>\u3001<code>removeAttribute(name)<\/code>\u3002<\/p>\n<p>\u90fd\u662f\u975e\u5e38\u5e38\u7528\u7684API\uff0c\u7136\u540e\u6211\u4e00\u76f4\u4ee5\u4e3a\u8bbe\u7f6e\u5c5e\u6027\u7684DOM API\u5c31\u8fd9\u51e0\u4e2a\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5076\u7136\u95f4\uff0c\u6211\u7a81\u7136\u53d1\u73b0\uff0c\u4e0d\u77e5\u9053\u4ec0\u4e48\u65f6\u5019\u8d77\uff0c\u5404\u5927\u73b0\u4ee3\u6d4f\u89c8\u5668\u5747\u652f\u6301\u4e86\u4e00\u4e2a\u5168\u65b0\u7684\u4e0eDOM\u5c5e\u6027\u76f8\u5173\u7684API <code>toggleAttribute(name [, force])<\/code>\u3002<\/p>\n<p>\u6211\u53bbcaniuse\u4e0a\u641c\u4e86\u4e0b\uff0c\u65e0\u8bba\u662fEdge\uff0cChrome\uff0cFirefox\u8fd8\u662fSafari\u6d4f\u89c8\u5668\uff0c\u5747\u662f2018\u5e74\u7b2c4\u5b63\u5ea6\u5f00\u59cb\u652f\u6301\u7684\uff0c\u50cf\u662f\u7ea6\u597d\u4e86\u4e00\u6837\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202012\/2020-12-05_223843.png\" width=\"637\" height=\"273\" alt=\"toggleAttribute\u5c5e\u6027\u517c\u5bb9\u6027\u8868\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u771f\u7684\u5c31\u662f\u6084\u65e0\u58f0\u606f\u5c31\u51fa\u73b0\u7684\uff0c\u60f3\u60f3\uff0cDOM API\u8fd8\u771f\u662f\u53ef\u60b2\uff0c\u6d4f\u89c8\u5668\u652f\u6301\u4e86\u65e0\u4eba\u95ee\u6d25\uff0c\u5728\u524d\u7aef\u5708\u5b50\u91cc\u6ca1\u6709\u8361\u8d77\u4e00\u4e1d\u6d9f\u6f2a\uff1b\u800cJS\u65b0\u7684\u8bed\u6cd5\u7cd6\u6d4f\u89c8\u5668\u652f\u6301\u4e86\uff0c\u5708\u5b50\u91cc\u6b22\u547c\u96c0\u8dc3\uff0c\u6587\u7ae0\u98de\u8d77\u3002\u7528\u4e00\u5e45\u56fe\u8868\u793a\u5c31\u50cf\u662f\u8fd9\u6837\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202012\/when-a-js-api.jpg\" width=\"243\" height=\"363\" alt=\"\u5f53\u51fa\u73b0\u4e00\u4e2a\u65b0\u7684API\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e8c\u3001HTML\u903b\u8f91\u5c5e\u6027\u8bbe\u7f6e\u4e13\u5bb6<\/h3>\n<p><code>toggleAttribute()<\/code>\u662fHTML\u5143\u7d20\u4e2d\u63a7\u5236\u903b\u8f91\u5c5e\u6027\u7684\u4e0d\u4e8c\u4e4b\u9009\u3002<\/p>\n<p>HTML\u4e2d\u5e38\u89c1\u7684\u903b\u8f91\u5c5e\u6027\u6709\uff1a<code>disabled<\/code>\u3001<code>readonly<\/code>\u3001<code>selected<\/code>\u3001<code>checked<\/code>\u3001<code>open<\/code>\uff08<code>&lt;dialog&gt;<\/code>\u3001<code>&lt;details&gt;<\/code>\u5143\u7d20\u4f7f\u7528\uff09\u3001<code>novalidate<\/code>\uff08<code>&lt;form&gt;<\/code>\u5143\u7d20\u4f7f\u7528\uff09\u3001<code>required<\/code>\u3001<code>reversed<\/code>\uff08<code>&lt;ul&gt;<\/code>\u3001<code>&lt;ol&gt;<\/code>\u5143\u7d20\u4f7f\u7528\uff09\u7b49\u3002<\/p>\n<p>\u5728\u8fc7\u53bb\uff0c\u6dfb\u52a0\u4e00\u4e2a\u5e03\u5c14\u5c5e\u6027\uff0c\u6211\u4eec\u90fd\u662f\u4f7f\u7528\u7c7b\u4f3c\u4e0b\u9762\u7684JS\u4ee3\u7801\uff1a<\/p>\n<pre>dialog.setAttribute('open', '');<\/pre>\n<p>\u5220\u9664\u4e00\u4e2a\u5e03\u5c14\u5c5e\u6027\u503c\u5219\u662f\u4f7f\u7528\uff1a<\/p>\n<pre>dialog.removeAttribute('open');<\/pre>\n<p>\u800ctoggle\u5207\u6362\u4e00\u4e2a\u5e03\u5c14\u5c5e\u6027\u503c\uff0c\u5219\u9700\u8981JS\u5148\u5224\u65ad\u5f53\u524d\u5c5e\u6027\u7684\u5e03\u5c14\u72b6\u6001\uff0c\u7136\u540e\u518d\u9009\u62e9\u662f<code>setAttribute<\/code>\u8fd8\u662f<code>removeAttribute<\/code>\uff0c\u8fd8\u662f\u5f88\u5570\u55e6\u7684\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u6709\u4e86<code>toggleAttribute()<\/code>\u65b9\u6cd5\uff0c\u6240\u6709\u7684\u5e03\u5c14\u5c5e\u6027\u7684\u64cd\u4f5c\u5168\u90e8\u90fd\u53ea\u9700\u89811\u4e2aAPI\u5c31\u641e\u5b9a\u4e86\u3002<\/p>\n<h4>\u4e3e\u4f8b\u8bf4\u660e<\/h4>\n<p>\u4f8b\u5982\uff0c\u6dfb\u52a0\u4e00\u4e2a\u5e03\u5c14\u5c5e\u6027\uff0c\u53ef\u4ee5\u8fd9\u6837\u8bbe\u7f6e\uff1a<\/p>\n<pre>dialog.toggleAttribute('open', true);<\/pre>\n<p>\u5220\u9664\u4e00\u4e2a\u5e03\u5c14\u5c5e\u6027\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>toggleAttribute<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre>dialog.toggleAttribute('open', false);<\/pre>\n<p>\u800c\u5207\u6362\u4e00\u4e2a\u5e03\u5c14\u5c5e\u6027\u503c\uff0c\u5219\u66f4\u50cf\u662f<code>toggleAttribute()<\/code>\u65b9\u7684\u672c\u804c\u5de5\u4f5c\u4e86\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ \u5982\u679c\u539f\u6765\u6709open\u5c5e\u6027\uff0c\u5219\u79fb\u9664\uff1b\u6ca1\u6709open\u5c5e\u6027\u5219\u6dfb\u52a0<\/span>\r\ndialog.toggleAttribute('open');<\/pre>\n<p>\u4e00\u4e2aAPI\u65b9\u6cd5\u901a\u6740\uff0c\u4ee3\u7801\u5927\u5927\u7b80\u5316\uff0c\u5b9e\u7528\u81f3\u6781\u3002<\/p>\n<p><span class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4f53\u9a8c\u7cdf\u7cd5\u7684\u5783\u573e\u76d7\u7248\u7f51\u7ad9\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u539f\u6587\u83b7\u5f97\u5f88\u597d\u7684\u4f53\u9a8c\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=9727\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<h3>\u4e09\u3001\u8bed\u6cd5\u3001\u53c2\u6570\u548c\u5b9e\u4f8b<\/h3>\n<p><code>toggleAttribute()<\/code>\u65b9\u6cd5\u7684\u8bed\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>Element.toggleAttribute(name [, force]);<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<dl>\n<dt><strong>name<\/strong><\/dt>\n<dd>\u9700\u8981\u5207\u6362\u663e\u793a\u7684\u5e03\u5c14\u5c5e\u6027\u540d\u79f0\uff0c\u53ef\u4ee5\u662f\u81ea\u5b9a\u4e49\u7684\u89c4\u8303\u4e2d\u6ca1\u6709\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>document.body.toggleAttribute('zhangxinxu');<\/pre>\n<p>\u5c31\u4f1a\u6709\u5982\u4e0b\u622a\u56fe\u6240\u793a\u7684\u6548\u679c\uff0c\u4f1a\u770b\u5230<code>&lt;body&gt;<\/code>\u5143\u7d20\u4e0a\u591a\u4e86\u4e2a<code>'zhangxinxu'<\/code>\u7684\u5c5e\u6027\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202012\/attrite-toggle.gif\" width=\"224\" height=\"82\" alt=\"\u5c5e\u6027\u6dfb\u52a0\u6548\u679c\u793a\u610f\" class=\"alignnone size-medium\" \/>\n<\/dd>\n<dt><strong>force<\/strong><\/dt>\n<dd>\u5e03\u5c14\u503c\u3002<code>true<\/code>\u7684\u8bdd\u5c31\u662f\u5f3a\u5236\u8bbe\u7f6e\u8be5\u5c5e\u6027\u4e3a<code>true<\/code>\uff0c\u4e5f\u5c31\u662f\u6dfb\u52a0\u8be5\u5c5e\u6027\uff1b<code>false<\/code>\u7684\u8bdd\u5219\u8868\u793a\u79fb\u9664\u8be5\u5e03\u5c14\u5c5e\u6027\u3002<\/dd>\n<\/dl>\n<p>\u5f53\u6211\u4eec\u5e0c\u671b\u77e5\u9053\u6267\u884c<code>toggleAttribute()<\/code>\u65b9\u6cd5\u540e\uff0c\u5143\u7d20\u5230\u5e95\u6709\u6ca1\u6709\u8be5\u5c5e\u6027\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528<code>toggleAttribute()<\/code>\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p><strong>\u8fd4\u56de\u503c<\/strong><\/p>\n<pre>let isHasAttribute = Element.toggleAttribute(name);<\/pre>\n<p>\u5982\u679c<code>toggleAttribute()<\/code>\u65b9\u6cd5\u6267\u884c\u662f\u6dfb\u52a0\u5c5e\u6027\uff0c\u5219<code>isHasAttribute<\/code>\u503c\u662f<code>true<\/code>\uff0c\u5426\u5219\u5c31\u662f<code>false<\/code>\u3002<\/p>\n<h4>\u5b9e\u4f8b<\/h4>\n<p>\u7b80\u5355\u7684\u5b9e\u4f8b\u793a\u610f\u4e0b<code>toggleAttribute<\/code>\u7684\u4f7f\u7528\uff0c\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>&lt;input id=\"input\"&gt; &lt;button id=\"button\" value=\"\u542f\u7528\"&gt;\u7981\u7528&lt;\/button&gt;<\/pre>\n<pre>button.addEventListener('click', _ =&gt; {\r\n    input.toggleAttribute('disabled');\r\n});<\/pre>\n<p>\u5b9e\u73b0\u7684\u6548\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"30\" src=\"\/study\/202012\/toggle-attribute.html\" frameborder=\"0\"><\/iframe><\/p>\n<p>\u5176\u4e2d\uff0c\u70b9\u51fb\u6309\u94ae\u7684\u6587\u5b57\u53d8\u5316\u662f\u901a\u8fc7CSS\u9009\u62e9\u5668\u63a7\u5236\u7684\uff1a<\/p>\n<pre><span style=\"color:green;\">\/* \u6309\u94ae\u6587\u5b57\u53d8\u5316 *\/<\/span>\r\n:disabled ~ button {\r\n    -webkit-text-fill-color: transparent;\r\n}\r\n:disabled ~ button::before {\r\n    position: absolute;\r\n    content: attr(value);\r\n    -webkit-text-fill-color: currentColor;\r\n}<\/pre>\n<h3>\u56db\u3001Polyfill\u4e0e\u7ed3\u675f\u8bed<\/h3>\n<p>\u7531\u4e8e<code>toggleAttribute<\/code>\u8fd8\u7b97\u6bd4\u8f83\u65b0\uff0c\u51fa\u9053\u5230\u73b0\u5728\u624d2\u5e74\u65f6\u95f4\uff0c\u8fd8\u7b97\u65b0\u4eba\uff0c\u56e0\u6b64\uff0c\u76ee\u524d\u5728\u5bf9\u5916\u7684\u5927\u578b\u9879\u76ee\u4e2d\u76f4\u63a5\u4f7f\u7528\u8fd8\u4e0d\u592a\u5408\u9002\uff0c\u4e0d\u8fc7\uff0c\u4e0d\u8981\u62c5\u5fc3\uff0c\u5f15\u5165\u4e00\u6bb5\u5c0f\u5c0f\u7684Polyfill\u4ee3\u7801\u5c31\u53ef\u4ee5\u4e07\u4e8b\u5927\u5409\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u8fd9\u6bb5Polyfill\u4ee3\u7801\u6e90\u81ea<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Element\/toggleAttribute\" rel=\"noopener\" target=\"_blank\">MDN\u6587\u6863<\/a>\uff1a<\/p>\n<pre>if (!Element.prototype.toggleAttribute) {\r\n  Element.prototype.toggleAttribute = function(name, force) {\r\n    if(force !== void 0) force = !!force \r\n    \r\n    if (this.hasAttribute(name)) {\r\n      if (force) return true;\r\n\r\n      this.removeAttribute(name);\r\n      return false;\r\n    }\r\n    if (force === false) return false;\r\n      \r\n    this.setAttribute(name, \"\");\r\n    return true;\r\n  };\r\n}<\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u653e\u5728\u4e1a\u52a1\u4ee3\u7801\u524d\u9762\u4efb\u610f\u4f4d\u7f6e\u90fd\u53ef\u4ee5\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u60f3\u5230\u4e86\u4e00\u6bb5\u5e7f\u544a\u8bed\u732e\u7ed9\u5e7f\u5927\u7684\u5e03\u5c14\u5c5e\u6027\u4eec\u3002<\/p>\n<p>\u201ctoggleAttribute\uff0c\u4f60\u503c\u5f97\u62e5\u6709\uff01\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202012\/toggle-attribute-cover.png\" width=\"240\" height=\"166\" alt=\"\u4f60\u503c\u5f97\u62e5\u6709\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u539a\u539a~~ <img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f61d.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9727\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9727<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202012\/toggle-attribute-cover.png\" width=\"160\" height=\"111\" alt=\"\u4f60\u503c\u5f97\u62e5\u6709\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u5404\u5927\u73b0\u4ee3\u6d4f\u89c8\u5668\u57282018\u5e74\u5e95\u5747\u652f\u6301\u4e86\u4e00\u4e2a\u540d\u4e3atoggleAttribute\u7684DOM API\uff0c\u4e13\u4e3aHTML\u5e03\u5c14\u5c5e\u6027\u8bbe\u8ba1\uff0c\u975e\u5e38\u5b9e\u7528\uff0c\u4e0d\u5bb9\u9519\u8fc7\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[716,1693,13,1389,1052,1694,546,694,1453,1692],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9727"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=9727"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9727\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}