{"id":170997,"date":"2024-05-08T17:19:56","date_gmt":"2024-05-08T09:19:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/170997.html"},"modified":"2024-05-08T17:20:01","modified_gmt":"2024-05-08T09:20:01","slug":"javascript-%e9%a2%9c%e8%89%b2%e5%88%87%e6%8d%a2%ef%bc%8c%e6%80%8e%e4%b9%88%e5%9c%a8%e4%b8%a4%e4%b8%aa%e9%a2%9c%e8%89%b2%e4%b9%8b%e9%97%b4%e5%88%87%e6%8d%a2%e5%91%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/170997.html","title":{"rendered":"javascript  \u989c\u8272\u5207\u6362\uff0c\u600e\u4e48\u5728\u4e24\u4e2a\u989c\u8272\u4e4b\u95f4\u5207\u6362\u5462"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27042025\/21076454-331c-40fd-914e-854e762abbd7.webp\" alt=\"javascript  \u989c\u8272\u5207\u6362\uff0c\u600e\u4e48\u5728\u4e24\u4e2a\u989c\u8272\u4e4b\u95f4\u5207\u6362\u5462\" \/><\/p>\n<p><p>JavaScript\u7684\u989c\u8272\u5207\u6362\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u811a\u672c\u5b9e\u73b0\uff0c\u8ba9\u7528\u6237\u80fd\u5728\u4e24\u4e2a\uff08\u6216\u66f4\u591a\uff09\u989c\u8272\u4e4b\u95f4\u5207\u6362\u3002<strong>\u4e3b\u8981\u65b9\u6cd5\u662f\u4f7f\u7528JavaScript\u53bb\u4fee\u6539HTML\u5143\u7d20\u7684CSS\u6837\u5f0f<\/strong>\u3001<strong>\u5229\u7528\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u54cd\u5e94\u7528\u6237\u64cd\u4f5c<\/strong>\uff0c\u4ee5\u53ca<strong>\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u5b9a\u4f55\u65f6\u66f4\u6539\u989c\u8272<\/strong>\u3002\u6700\u5e38\u7528\u7684\u5b9e\u73b0\u65b9\u5f0f\u662f\u5728\u5143\u7d20\u7684\u4e8b\u4ef6\uff08\u5982\u70b9\u51fb\uff09\u53d1\u751f\u65f6\uff0c\u901a\u8fc7JavaScript\u66f4\u6539\u5143\u7d20\u7684&#039;class&#039;\u5c5e\u6027\u6216\u76f4\u63a5\u64cd\u4f5c\u5176&#039;style&#039;\u5c5e\u6027\uff0c\u6765\u6539\u53d8\u5176\u80cc\u666f\u8272\u6216\u6587\u672c\u8272\u3002<\/p>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7406\u89e3DOM\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0cDOM\uff08Document Object Model\uff09\u662f\u4e00\u4e2a\u7f16\u7a0b\u63a5\u53e3\uff0c\u5b83\u5141\u8bb8\u811a\u672c\u52a8\u6001\u5730\u8bbf\u95ee\u548c\u4fee\u6539\u6587\u6863\u7684\u5185\u5bb9\u3001\u7ed3\u6784\u548c\u6837\u5f0f\u3002\u8981\u5b9e\u73b0\u989c\u8272\u5207\u6362\uff0c\u4f60\u9700\u8981\u901a\u8fc7JavaScript\u6765\u83b7\u53d6\u9700\u8981\u66f4\u6539\u989c\u8272\u7684HTML\u5143\u7d20\uff0c\u5e76\u5bf9\u5b83\u4eec\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u83b7\u53d6HTML\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u83b7\u53d6\u9700\u8981\u64cd\u4f5c\u7684HTML\u5143\u7d20\uff0c\u5982\u4f7f\u7528<code>document.getElementById()<\/code>\u3001<code>document.querySelector()<\/code>\u6216<code>document.querySelectorAll()<\/code>\u7b49\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let element = document.getElementById(&#039;myElement&#039;); \/\/ \u901a\u8fc7ID\u83b7\u53d6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4fee\u6539CSS\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5143\u7d20\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u5143\u7d20\u7684<code>style<\/code>\u5c5e\u6027\u6765\u6539\u53d8\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">element.style.backgroundColor = &#039;red&#039;; \/\/ \u76f4\u63a5\u4fee\u6539\u80cc\u666f\u989c\u8272<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u4e8b\u4ef6\u76d1\u542c\u5668<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u989c\u8272\u7684\u4ea4\u4e92\u5f0f\u5207\u6362\uff0c\u4f60\u9700\u8981\u7ed1\u5b9a\u4e8b\u4ef6\u76d1\u542c\u5668\u5230\u7528\u6237\u53ef\u80fd\u4ea4\u4e92\u7684\u5143\u7d20\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>\u7ed1\u5b9a\u70b9\u51fb\u4e8b\u4ef6<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>addEventListener<\/code>\u65b9\u6cd5\u6765\u4e3a\u6307\u5b9a\u5143\u7d20\u7ed1\u5b9a\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">element.addEventListener(&#039;click&#039;, function() {<\/p>\n<p>  \/\/ \u5728\u8fd9\u91cc\u7f16\u5199\u70b9\u51fb\u5143\u7d20\u540e\u6267\u884c\u7684\u4ee3\u7801<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5207\u6362\u989c\u8272\u7684\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u6761\u4ef6\u8bed\u53e5\u6765\u5207\u6362\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">element.addEventListener(&#039;click&#039;, function() {<\/p>\n<p>  if (element.style.backgroundColor === &#039;red&#039;) {<\/p>\n<p>    element.style.backgroundColor = &#039;blue&#039;;<\/p>\n<p>  } else {<\/p>\n<p>    element.style.backgroundColor = &#039;red&#039;;<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u989c\u8272\u5207\u6362\u903b\u8f91<\/h2>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u989c\u8272\u5207\u6362\u903b\u8f91\u7684\u51fd\u6570\u3002\u8be5\u51fd\u6570\u4f1a\u68c0\u67e5\u5f53\u524d\u7684\u989c\u8272\uff0c\u5e76\u6839\u636e\u5f53\u524d\u72b6\u6001\u5207\u6362\u5230\u53e6\u4e00\u4e2a\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h3>\u5224\u65ad\u548c\u5207\u6362<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u6765\u5224\u65ad\u5f53\u524d\u989c\u8272\u5e76\u5207\u6362\u5230\u53e6\u4e00\u79cd\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function toggleColor() {<\/p>\n<p>  if (element.style.backgroundColor === &#039;red&#039;) {<\/p>\n<p>    element.style.backgroundColor = &#039;blue&#039;;<\/p>\n<p>  } else {<\/p>\n<p>    element.style.backgroundColor = &#039;red&#039;;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u5316\u5207\u6362\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u51fd\u6570\uff0c\u4f7f\u5176\u66f4\u52a0\u901a\u7528\uff0c\u80fd\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5143\u7d20\u548c\u989c\u8272\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function changeColor(element, color1, color2) {<\/p>\n<p>  element.style.backgroundColor = element.style.backgroundColor === color1 ? color2 : color1;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u63d0\u5347\u7528\u6237\u4f53\u9a8c<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u989c\u8272\u7684\u5207\u6362\uff0c\u4f60\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u529f\u80fd\u6765\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u6bd4\u5982\u52a8\u753b\u6548\u679c\u3001\u97f3\u4e50\u64ad\u653e\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u6dfb\u52a0CSS\u7c7b<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5207\u6362\u5143\u7d20\u7684CSS\u7c7b\u6765\u5f15\u5165\u66f4\u590d\u6742\u7684\u6837\u5f0f\u6539\u53d8\uff0c\u5305\u62ec\u52a8\u753b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">element.classList.toggle(&#039;myClass&#039;); \/\/ &#039;myClass&#039;\u662f\u5df2\u5728CSS\u4e2d\u5b9a\u4e49\u7684\u4e00\u4e2a\u7c7b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u63d0\u4f9b\u53cd\u9988<\/h3>\n<\/p>\n<p><p>\u5728\u989c\u8272\u5207\u6362\u65f6\uff0c\u7ed9\u7528\u6237\u4e00\u4e9b\u5f62\u5f0f\u7684\u53cd\u9988\uff0c\u4f8b\u5982\u6539\u53d8\u6587\u672c\u7684\u5185\u5bb9\u6216\u989c\u8272\u6765\u544a\u77e5\u7528\u6237\u989c\u8272\u5df2\u7ecf\u5207\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">element.textContent = `\u80cc\u666f\u8272\u662f: ${element.style.backgroundColor}`; \/\/ \u66f4\u65b0\u6587\u672c\u5185\u5bb9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\uff0cJavaScript\u4e2d\u5b9e\u73b0\u989c\u8272\u5207\u6362\u7684\u6838\u5fc3\u662f\u7406\u89e3\u548c\u4f7f\u7528DOM\u64cd\u4f5c\u3001\u4e8b\u4ef6\u76d1\u542c\u4ee5\u53ca\u6761\u4ef6\u903b\u8f91\u3002\u901a\u8fc7\u5728HTML\u5143\u7d20\u4e0a\u6355\u6349\u5e76\u54cd\u5e94\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u65b9\u5f0f\u63a7\u5236\u989c\u8272\u7684\u53d8\u5316\uff0c\u4ece\u800c\u4e3a\u7528\u6237\u63d0\u4f9b\u4e00\u4e2a\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u7684\u4f53\u9a8c\u3002\u901a\u8fc7\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u548c\u7ec6\u8282\u6765\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\uff0c\u4f60\u7684\u989c\u8272\u5207\u6362\u529f\u80fd\u5c06\u66f4\u52a0\u5438\u5f15\u7528\u6237\u7684\u6ce8\u610f\uff0c\u5e76\u4f7f\u5f97\u4f60\u7684\u7f51\u9875\u66f4\u663e\u751f\u52a8\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u5b9e\u73b0\u989c\u8272\u5207\u6362\u6548\u679c\uff1f<\/strong><\/p>\n<ol>\n<li><strong>\u4f7f\u7528 CSS \u7c7b\u540d\u5207\u6362\u6548\u679c\uff1a<\/strong> \u5728 HTML \u5143\u7d20\u4e0a\u4e3a\u4e0d\u540c\u989c\u8272\u5b9a\u4e49\u4e0d\u540c\u7684 CSS \u7c7b\u540d\uff0c\u7136\u540e\u4f7f\u7528 JavaScript \u52a8\u6001\u5207\u6362\u5143\u7d20\u7684\u7c7b\u540d\u5373\u53ef\u5b9e\u73b0\u989c\u8272\u5207\u6362\u6548\u679c\u3002<\/li>\n<\/ol>\n<p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e24\u4e2a CSS \u7c7b\u540d <code>.color1<\/code> \u548c <code>.color2<\/code> \u5206\u522b\u8868\u793a\u4e0d\u540c\u7684\u989c\u8272\u6837\u5f0f\uff0c\u5728 JavaScript \u4e2d\u901a\u8fc7\u6dfb\u52a0\u6216\u79fb\u9664\u8fd9\u4e24\u4e2a\u7c7b\u540d\u6765\u5b9e\u73b0\u989c\u8272\u5207\u6362\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;style&gt;\n.color1 {\n  color: red;\n}\n\n.color2 {\n  color: blue;\n}\n&lt;\/style&gt;\n\n&lt;p id=&quot;text&quot;&gt;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c\u3002&lt;\/p&gt;\n\n&lt;script&gt;\nvar textElement = document.getElementById(&quot;text&quot;);\nvar color1Class = &quot;color1&quot;;\nvar color2Class = &quot;color2&quot;;\nvar toggle = false;\n\nfunction toggleColor() {\n  if (toggle) {\n    textElement.classList.remove(color2Class);\n    textElement.classList.add(color1Class);\n  } else {\n    textElement.classList.remove(color1Class);\n    textElement.classList.add(color2Class);\n  }\n  toggle = !toggle;\n}\n\nsetInterval(toggleColor, 1000); \/\/ \u6bcf\u96941\u79d2\u5207\u6362\u4e00\u6b21\u989c\u8272\n&lt;\/script&gt;\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528 JavaScript \u7684\u5185\u7f6e\u989c\u8272\u51fd\u6570\uff1a<\/strong> JavaScript \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u989c\u8272\u51fd\u6570\uff0c\u4f8b\u5982 <code>rgb()<\/code>\u3001 <code>rgba()<\/code>\u3001 <code>hsl()<\/code>\u3001 <code>hsla()<\/code> \u7b49\uff0c\u901a\u8fc7\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u5e76\u4f20\u9012\u4e0d\u540c\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u989c\u8272\u7684\u5207\u6362\u3002<\/li>\n<\/ol>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>setInterval()<\/code> \u51fd\u6570\u548c <code>Math.random()<\/code> \u51fd\u6570\u751f\u6210\u968f\u673a\u989c\u8272\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;p id=&quot;text&quot;&gt;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c\u3002&lt;\/p&gt;\n\n&lt;script&gt;\nvar textElement = document.getElementById(&quot;text&quot;);\n\nfunction getRandomColor() {\n  var r = Math.floor(Math.random() * 256);\n  var g = Math.floor(Math.random() * 256);\n  var b = Math.floor(Math.random() * 256);\n  return &quot;rgb(&quot; + r + &quot;, &quot; + g + &quot;, &quot; + b + &quot;)&quot;;\n}\n\nfunction changeColor() {\n  textElement.style.color = getRandomColor();\n}\n\nsetInterval(changeColor, 1000); \/\/ \u6bcf\u96941\u79d2\u5207\u6362\u4e00\u6b21\u989c\u8272\n&lt;\/script&gt;\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528 CSS \u52a8\u753b\u5b9e\u73b0\u6e10\u53d8\u6548\u679c\uff1a<\/strong> \u5728 CSS \u4e2d\u4f7f\u7528 <code>@keyframes<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u4e00\u4e2a\u989c\u8272\u6e10\u53d8\u7684\u52a8\u753b\uff0c\u7136\u540e\u5728 HTML \u5143\u7d20\u4e0a\u5e94\u7528\u8fd9\u4e2a\u52a8\u753b\u5373\u53ef\u5b9e\u73b0\u989c\u8272\u5207\u6362\u7684\u6548\u679c\u3002<\/li>\n<\/ol>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a <code>colorChange<\/code> \u7684\u52a8\u753b\uff0c\u901a\u8fc7\u6539\u53d8 <code>color<\/code> \u5c5e\u6027\u7684\u503c\u5b9e\u73b0\u989c\u8272\u6e10\u53d8\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;style&gt;\n@keyframes colorChange {\n  0% { color: red; }\n  50% { color: blue; }\n  100% { color: red; }\n}\n\n.text {\n  animation: colorChange 3s infinite;\n}\n&lt;\/style&gt;\n\n&lt;p class=&quot;text&quot;&gt;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u672c\u3002&lt;\/p&gt;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"JavaScript\u7684\u989c\u8272\u5207\u6362\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u811a\u672c\u5b9e\u73b0\uff0c\u8ba9\u7528\u6237\u80fd\u5728\u4e24\u4e2a\uff08\u6216\u66f4\u591a\uff09\u989c\u8272\u4e4b\u95f4\u5207\u6362\u3002\u4e3b\u8981\u65b9\u6cd5\u662f\u4f7f\u7528Ja [&hellip;]","protected":false},"author":3,"featured_media":171004,"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\/170997"}],"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=170997"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/170997\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/171004"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=170997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=170997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=170997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}