{"id":169064,"date":"2024-05-08T16:37:48","date_gmt":"2024-05-08T08:37:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/169064.html"},"modified":"2024-05-08T16:37:53","modified_gmt":"2024-05-08T08:37:53","slug":"javascript-%e5%b1%9e%e6%80%a7%e8%8a%82%e7%82%b9%e6%9c%89%e5%93%aa%e4%ba%9b%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/169064.html","title":{"rendered":"JavaScript \u5c5e\u6027\u8282\u70b9\u6709\u54ea\u4e9b\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27035401\/aa272308-abc5-457b-8451-df4da7f50c70.webp\" alt=\"JavaScript \u5c5e\u6027\u8282\u70b9\u6709\u54ea\u4e9b\u7c7b\u578b\" \/><\/p>\n<p><p>JavaScript \u4e2d\u7684\u5c5e\u6027\u8282\u70b9\u4e3b\u8981\u5206\u4e3a\u4e09\u5927\u7c7b\uff1a<strong>\u5143\u7d20\u5c5e\u6027\u3001\u6587\u672c\u5c5e\u6027\u3001\u53ca\u6837\u5f0f\u5c5e\u6027<\/strong>\u3002\u6bcf\u4e00\u79cd\u7c7b\u578b\u7684\u5c5e\u6027\u8282\u70b9\u90fd\u5728 DOM\uff08Document Object Model\uff09\u64cd\u4f5c\u4e2d\u626e\u6f14\u7740\u4e0d\u540c\u89d2\u8272\uff0c\u4f7f\u5f97\u7f51\u9875\u80fd\u591f\u901a\u8fc7 JavaScript \u52a8\u6001\u5730\u8fdb\u884c\u5185\u5bb9\u3001\u6837\u5f0f\u4ee5\u53ca\u529f\u80fd\u4e0a\u7684\u8c03\u6574\u548c\u4fee\u6539\u3002<strong>\u5143\u7d20\u5c5e\u6027<\/strong>\u8282\u70b9\u662f\u5728 HTML \u548c JavaScript \u4ea4\u4e92\u4e2d\u6700\u5e38\u7528\u5230\u7684\uff0c\u5b83\u4eec\u4ee3\u8868\u4e86 HTML \u5143\u7d20\u4e0a\u7684\u5c5e\u6027\uff0c\u4f8b\u5982 id\u3001class\u3001src\u3001href \u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5143\u7d20\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5143\u7d20\u5c5e\u6027\u662f\u76f4\u63a5\u9644\u52a0\u5728 HTML \u6807\u7b7e\u4e0a\u7684\u5c5e\u6027\uff0c\u5b83\u4eec\u5b9a\u4e49\u4e86\u5143\u7d20\u7684\u57fa\u672c\u4fe1\u606f\u548c\u884c\u4e3a\u3002\u6bd4\u5982\uff0c<code>&lt;a&gt;<\/code> \u6807\u7b7e\u7684 <code>href<\/code> \u5c5e\u6027\u5b9a\u4e49\u4e86\u94fe\u63a5\u7684\u76ee\u6807 URL\uff0c<code>&lt;img&gt;<\/code> \u6807\u7b7e\u7684 <code>src<\/code> \u5c5e\u6027\u51b3\u5b9a\u4e86\u56fe\u7247\u7684\u6765\u6e90\u3002<\/p>\n<\/p>\n<p><p>\u5728 JavaScript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u64cd\u4f5c DOM \u6765\u8bfb\u53d6\u3001\u4fee\u6539\u3001\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u7684\u5c5e\u6027\u3002\u4f7f\u7528 <code>getAttribute()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6307\u5b9a\u5143\u7d20\u7684\u5c5e\u6027\u503c\uff0c<code>setAttribute()<\/code> \u65b9\u6cd5\u5141\u8bb8\u4fee\u6539\u6216\u6dfb\u52a0\u5c5e\u6027\uff0c\u800c <code>removeAttribute()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u5220\u9664\u5c5e\u6027\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5e94\u7528\uff0c\u6211\u4eec\u80fd\u591f\u52a8\u6001\u5730\u6539\u53d8\u7f51\u9875\u4e0a\u5143\u7d20\u7684\u7279\u5f81\u548c\u884c\u4e3a\uff0c\u8fdb\u800c\u63d0\u5347\u7528\u6237\u4ea4\u4e92\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6587\u672c\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u6587\u672c\u5c5e\u6027\u5173\u7cfb\u5230 HTML \u6587\u6863\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002\u6587\u672c\u8282\u70b9\u901a\u5e38\u88ab\u89c6\u4e3a\u5143\u7d20\u8282\u70b9\u7684\u5b50\u8282\u70b9\u51fa\u73b0\uff0c\u5b83\u4eec\u5305\u542b\u7740\u5143\u7d20\u6807\u7b7e\u4e4b\u95f4\u7684\u5177\u4f53\u6587\u672c\u4fe1\u606f\u3002\u5728 DOM \u6811\u4e2d\uff0c\u6587\u672c\u5c5e\u6027\u4f7f\u5f97\u6587\u672c\u5185\u5bb9\u80fd\u591f\u88ab JavaScript \u6240\u8bc6\u522b\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5229\u7528 <code>textContent<\/code> \u5c5e\u6027\u6216 <code>innerText<\/code> \u5c5e\u6027\uff0c\u53ef\u4ee5\u83b7\u53d6\u6216\u8bbe\u7f6e\u6307\u5b9a\u5143\u7d20\u5185\u90e8\u7684\u6587\u672c\u5185\u5bb9\u3002\u8fd9\u4e24\u4e2a\u5c5e\u6027\u867d\u7136\u90fd\u80fd\u8fbe\u5230\u76f8\u4f3c\u7684\u6548\u679c\uff0c\u4f46\u5728\u5904\u7406\u7a7a\u683c\u548c\u65b0\u589e\u884c\u65f6\u5b58\u5728\u5dee\u5f02\u3002<code>textContent<\/code> \u5c5e\u6027\u4f1a\u83b7\u53d6\u6240\u6709\u5143\u7d20\u7684\u5185\u5bb9\uff0c\u5305\u62ec <code>&lt;script&gt;<\/code> \u548c <code>&lt;style&gt;<\/code> \u5143\u7d20\uff0c\u540c\u65f6\u4fdd\u7559\u7a7a\u767d\u7b26\uff0c\u800c <code>innerText<\/code> \u53ea\u5173\u6ce8\u6e32\u67d3\u540e\u7684\u53ef\u89c1\u6587\u672c\u3002\u901a\u8fc7\u8fd9\u4e9b\u6587\u672c\u5c5e\u6027\u7684\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u7f51\u9875\u5185\u5bb9\u7684\u52a8\u6001\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6837\u5f0f\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u6837\u5f0f\u5c5e\u6027\u662f\u6307\u90a3\u4e9b\u5b9a\u4e49 HTML \u5143\u7d20\u5728\u9875\u9762\u4e2d\u5448\u73b0\u6837\u5f0f\u7684\u5c5e\u6027\u3002\u901a\u8fc7\u64cd\u4f5c\u8fd9\u4e9b\u5c5e\u6027\uff0c\u53ef\u4ee5\u52a8\u6001\u5730\u4fee\u6539\u7f51\u9875\u7684\u5e03\u5c40\u548c\u5916\u89c2\uff0c\u5982\u989c\u8272\u3001\u5927\u5c0f\u3001\u8fb9\u8ddd\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u5728 JavaScript \u4e2d\uff0c\u5143\u7d20\u7684 <code>style<\/code> \u5c5e\u6027\u63d0\u4f9b\u4e86\u4e00\u79cd\u4fee\u6539\u5143\u7d20\u6837\u5f0f\u7684\u9014\u5f84\u3002\u8fd9\u4e2a\u5c5e\u6027\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u5143\u7d20\u7684\u6240\u6709\u6837\u5f0f\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u901a\u8fc7 <code>element.style.backgroundColor = &#039;red&#039;<\/code> \u53ef\u4ee5\u5c06\u5143\u7d20\u7684\u80cc\u666f\u8272\u8bbe\u7f6e\u4e3a\u7ea2\u8272\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>classList<\/code> \u5c5e\u6027\u6765\u64cd\u4f5c\u5143\u7d20\u7684\u7c7b\u540d\uff0c\u5b9e\u73b0\u6837\u5f0f\u7684\u5207\u6362\u3002\u8fd9\u4e9b\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u53ef\u80fd\u6027\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u6839\u636e\u7528\u6237\u7684\u64cd\u4f5c\u6216\u5176\u4ed6\u903b\u8f91\u6765\u6539\u53d8\u9875\u9762\u7684\u89c6\u89c9\u8868\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u5e94\u7528\u573a\u666f<\/strong> \u5728\u7f51\u7ad9\u5f00\u53d1\u4e2d\u5c24\u4e3a\u91cd\u8981\uff0c\u4e0d\u540c\u7c7b\u578b\u7684\u5c5e\u6027\u8282\u70b9\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e2d\u53d1\u6325\u4f5c\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e2a\u6027\u5316\u7528\u6237\u754c\u9762<\/strong><\/p>\n<p>\u901a\u8fc7\u64cd\u4f5c\u5143\u7d20\u5c5e\u6027\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7528\u6237\u754c\u9762\u7684\u4e2a\u6027\u5316\u8bbe\u7f6e\u3002\u6bd4\u5982\uff0c\u6839\u636e\u7528\u6237\u7684\u559c\u597d\u52a8\u6001\u66f4\u6362\u4e3b\u9898\u989c\u8272\uff0c\u6216\u6839\u636e\u4e0d\u540c\u7684\u64cd\u4f5c\u663e\u793a\u4e0d\u540c\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5185\u5bb9\u7ba1\u7406<\/strong><\/p>\n<p>\u6587\u672c\u5c5e\u6027\u7684\u64cd\u4f5c\u4f7f\u5185\u5bb9\u7ba1\u7406\u53d8\u5f97\u7075\u6d3b\u3002\u65e0\u8bba\u662f\u5185\u5bb9\u7684\u52a8\u6001\u52a0\u8f7d\u3001\u5b9e\u65f6\u66f4\u65b0\uff0c\u4ea6\u6216\u662f\u591a\u8bed\u8a00\u5207\u6362\uff0c\u90fd\u80fd\u591f\u901a\u8fc7\u6587\u672c\u5c5e\u6027\u7684\u64cd\u4f5c\u5f97\u4ee5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u54cd\u5e94\u5f0f\u5e03\u5c40<\/strong><\/p>\n<p>\u901a\u8fc7\u6837\u5f0f\u5c5e\u6027\u7684\u8c03\u6574\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u8bbe\u5907\u7684\u5c4f\u5e55\u5927\u5c0f\u548c\u5206\u8fa8\u7387\u8fdb\u884c\u5e03\u5c40\u7684\u52a8\u6001\u8c03\u6574\uff0c\u5b9e\u73b0\u54cd\u5e94\u5f0f\u7f51\u9875\u8bbe\u8ba1\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>JavaScript \u5c5e\u6027\u8282\u70b9\u7684\u64cd\u4f5c\u4e0d\u4ec5\u4ec5\u5c40\u9650\u4e8e\u8fd9\u4e9b\u5e94\u7528\u573a\u666f\u3002\u968f\u7740 Web \u6280\u672f\u7684\u53d1\u5c55\uff0c\u5b83\u4eec\u5728\u7f51\u9875\u52a8\u6001\u4ea4\u4e92\u548c\u89c6\u89c9\u8868\u73b0\u4e0a\u7684\u4f5c\u7528\u65e5\u76ca\u91cd\u8981\u3002\u638c\u63e1\u4e0d\u540c\u7c7b\u578b\u7684\u5c5e\u6027\u8282\u70b9\u53ca\u5176\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5bf9\u4e8e\u524d\u7aef\u5f00\u53d1\u4eba\u5458\u6765\u8bf4\u662f\u57fa\u672c\u6280\u80fd\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. JavaScript\u5c5e\u6027\u8282\u70b9\u6709\u54ea\u4e9b\u7c7b\u578b\uff1f<\/strong><\/p>\n<p>\u5c5e\u6027\u8282\u70b9\u5728JavaScript\u4e2d\u6709\u591a\u79cd\u7c7b\u578b\uff0c\u5305\u62ec\u5143\u7d20\u8282\u70b9\u3001\u6587\u672c\u8282\u70b9\u3001\u5c5e\u6027\u8282\u70b9\u548c\u6ce8\u91ca\u8282\u70b9\u7b49\u3002\u5c5e\u6027\u8282\u70b9\u662f\u5176\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u8282\u70b9\u7c7b\u578b\uff0c\u7528\u6765\u8868\u793aHTML\u5143\u7d20\u7684\u5c5e\u6027\u4fe1\u606f\u3002<\/p>\n<p><strong>2. JavaScript\u4e2d\u7684\u5c5e\u6027\u8282\u70b9\u6709\u54ea\u4e9b\u79cd\u7c7b\uff1f<\/strong><\/p>\n<p>\u5728JavaScript\u4e2d\uff0c\u5c5e\u6027\u8282\u70b9\u7684\u79cd\u7c7b\u5305\u62ec\uff1a\u6807\u51c6\u5c5e\u6027\u8282\u70b9\u3001\u81ea\u5b9a\u4e49\u5c5e\u6027\u8282\u70b9\u548c\u547d\u540d\u7a7a\u95f4\u5c5e\u6027\u8282\u70b9\u3002\u6807\u51c6\u5c5e\u6027\u8282\u70b9\u662f\u6307HTML\u5143\u7d20\u672c\u8eab\u5c31\u5177\u5907\u7684\u5c5e\u6027\uff0c\u4f8b\u5982id\u3001class\u7b49\u3002\u81ea\u5b9a\u4e49\u5c5e\u6027\u8282\u70b9\u662f\u6307\u5f00\u53d1\u8005\u81ea\u884c\u5b9a\u4e49\u7684\u5c5e\u6027\uff0c\u901a\u5e38\u4f7f\u7528data-\u524d\u7f00\u6765\u547d\u540d\uff0c\u4f8b\u5982data-name\u3001data-age\u7b49\u3002\u547d\u540d\u7a7a\u95f4\u5c5e\u6027\u8282\u70b9\u7528\u4e8e\u5904\u7406XML\u6587\u6863\u4e2d\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4f8b\u5982xmlns\u3001xml:lang\u7b49\u3002<\/p>\n<p><strong>3. JavaScript\u4e2d\u5c5e\u6027\u8282\u70b9\u7684\u4f7f\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><\/p>\n<p>\u5c5e\u6027\u8282\u70b9\u5728JavaScript\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u83b7\u53d6\u5c5e\u6027\u8282\u70b9\uff0c\u53ef\u4ee5\u4fee\u6539\u548c\u64cd\u4f5cHTML\u5143\u7d20\u7684\u5c5e\u6027\u503c\uff0c\u4ece\u800c\u5b9e\u73b0\u52a8\u6001\u6539\u53d8\u9875\u9762\u6837\u5f0f\u6216\u884c\u4e3a\u3002\u53e6\u5916\uff0c\u5c5e\u6027\u8282\u70b9\u4e5f\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u548c\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u5c5e\u6027\uff0c\u8fd9\u5bf9\u4e8e\u5b58\u50a8\u548c\u4f20\u9012\u989d\u5916\u7684\u6570\u636e\u4fe1\u606f\u975e\u5e38\u6709\u7528\u3002\u5728\u5904\u7406XML\u6587\u6863\u65f6\uff0c\u5c5e\u6027\u8282\u70b9\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u89e3\u6790\u548c\u7f16\u5199\u547d\u540d\u7a7a\u95f4\u76f8\u5173\u7684\u4ee3\u7801\u903b\u8f91\u3002\u603b\u4e4b\uff0c\u5c5e\u6027\u8282\u70b9\u5728JavaScript\u4e2d\u662f\u975e\u5e38\u7075\u6d3b\u548c\u5b9e\u7528\u7684\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript \u4e2d\u7684\u5c5e\u6027\u8282\u70b9\u4e3b\u8981\u5206\u4e3a\u4e09\u5927\u7c7b\uff1a\u5143\u7d20\u5c5e\u6027\u3001\u6587\u672c\u5c5e\u6027\u3001\u53ca\u6837\u5f0f\u5c5e\u6027\u3002\u6bcf\u4e00\u79cd\u7c7b\u578b\u7684\u5c5e\u6027\u8282\u70b9\u90fd\u5728 D [&hellip;]","protected":false},"author":3,"featured_media":169074,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169064"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=169064"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169064\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/169074"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=169064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=169064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=169064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}