{"id":3187,"date":"2024-11-10T14:20:08","date_gmt":"2024-11-10T06:20:08","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3187\/"},"modified":"2024-11-10T14:20:08","modified_gmt":"2024-11-10T06:20:08","slug":"js%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%a0%87%e7%ad%be%e5%b1%9e%e6%80%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3187\/","title":{"rendered":"js\u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u5c5e\u6027"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\uff0c\u83b7\u53d6\u6807\u7b7e\u5c5e\u6027\u7684\u65b9\u6cd5\u6709\uff1a\u83b7\u53d6\u6807\u7b7e\u5143\u7d20\u4f7f\u7528 .getattribute() \u65b9\u6cd5\u83b7\u53d6\u6307\u5b9a\u5c5e\u6027\u4f7f\u7528 getattributes() \u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u5c5e\u6027\u4f7f\u7528 hasattribute() \u65b9\u6cd5\u68c0\u67e5\u5e03\u5c14\u5c5e\u6027\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110806333382576.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u5c5e\u6027\u63d2\u56fe\" alt=\"js\u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u5c5e\u6027\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u83b7\u53d6\u6807\u7b7e\u5c5e\u6027<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u83b7\u53d6\u6807\u7b7e\u5c5e\u6027\uff1a<\/p>\n<ol>\n<li> <strong>\u83b7\u53d6\u6807\u7b7e\u5143\u7d20\uff1a<\/strong>\u4f7f\u7528 document.getElementById() \u6216 document.getElementsByTagName() \u83b7\u53d6\u8981\u83b7\u53d6\u5c5e\u6027\u7684\u6807\u7b7e\u5143\u7d20\u3002<\/li>\n<li> <strong>\u83b7\u53d6\u5c5e\u6027\uff1a<\/strong>\u4f7f\u7528 .getAttribute() \u65b9\u6cd5\u83b7\u53d6\u6807\u7b7e\u7684\u6307\u5b9a\u5c5e\u6027\u3002<\/li>\n<\/ol>\n<p>\u4f8b\u5982\uff0c\u8981\u83b7\u53d6<\/p>\n<p>  \u5143\u7d20\u7684 class \u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre>const myDiv = document.getElementById(\"myDiv\");\nconst className = myDiv.getAttribute(\"class\");<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u83b7\u53d6\u591a\u4e2a\u5c5e\u6027\uff1a<\/strong><\/p>\n<p>\u8981\u83b7\u53d6\u591a\u4e2a\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528 getAttributes() \u65b9\u6cd5\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5c5e\u6027\u7684 NamedNodeMap \u5bf9\u8c61\uff1a<\/p>\n<pre>const attributes = myDiv.getAttributes();\nfor (let i = 0; i &lt; attributes.length; i++) {\n  console.log(attributes[i].name, attributes[i].value);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u83b7\u53d6\u5e03\u5c14\u5c5e\u6027\uff1a<\/strong><\/p>\n<p>\u67d0\u4e9b\u5c5e\u6027\u662f\u5e03\u5c14\u7c7b\u578b\u7684\uff0c\u4f8b\u5982 checked \u6216 disabled\u3002\u5bf9\u4e8e\u5e03\u5c14\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528 hasAttribute() \u65b9\u6cd5\u68c0\u67e5\u5c5e\u6027\u662f\u5426\u5b58\u5728\uff1a<\/p>\n<pre>if (myDiv.hasAttribute(\"checked\")) {\n  \/\/ \u5143\u7d20\u5df2\u9009\u4e2d\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8bbe\u7f6e\u5c5e\u6027\uff1a<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 .setAttribute() \u65b9\u6cd5\u8bbe\u7f6e\u6807\u7b7e\u5c5e\u6027\uff1a<\/p>\n<pre>myDiv.setAttribute(\"class\", \"active\");<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u83b7\u53d6\u6807\u7b7e\u5c5e\u6027\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\uff0c\u83b7\u53d6\u6807\u7b7e\u5c5e\u6027\u7684\u65b9\u6cd5\u6709\uff1a\u83b7\u53d6\u6807\u7b7e\u5143\u7d20\u4f7f\u7528 .getattribute() \u65b9\u6cd5\u83b7\u53d6\u6307\u5b9a\u5c5e\u6027\u4f7f\u7528 getattributes() \u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u5c5e\u6027\u4f7f\u7528 hasattribute() \u65b9\u6cd5\u68c0\u67e5\u5e03\u5c14\u5c5e\u6027 \u5982\u4f55\u5728 JavaScript \u4e2d\u83b7\u53d6\u6807\u7b7e\u5c5e\u6027 \u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u83b7\u53d6\u6807\u7b7e\u5c5e\u6027\uff1a \u83b7\u53d6\u6807\u7b7e\u5143\u7d20\uff1a\u4f7f\u7528 document.getElementById() \u6216 document.getElementsByTagName() \u83b7\u53d6\u8981\u83b7\u53d6\u5c5e\u6027\u7684\u6807\u7b7e\u5143\u7d20\u3002 \u83b7\u53d6\u5c5e\u6027\uff1a\u4f7f\u7528 .getAttribute() \u65b9\u6cd5\u83b7\u53d6\u6807\u7b7e\u7684\u6307\u5b9a\u5c5e\u6027\u3002 \u4f8b\u5982\uff0c\u8981\u83b7\u53d6 \u5143\u7d20\u7684 class \u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a const myDiv = document.getElementById(&#8220;myDiv&#8221;); const className = myDiv.getAttribute(&#8220;class&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u83b7\u53d6\u591a\u4e2a\u5c5e\u6027\uff1a \u8981\u83b7\u53d6\u591a\u4e2a\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528 getAttributes() \u65b9\u6cd5\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5c5e\u6027\u7684 NamedNodeMap \u5bf9\u8c61\uff1a const attributes = myDiv.getAttributes(); for (let i = 0; i &lt; attributes.length; i++) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3187","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3187"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3187\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}