{"id":270904,"date":"2024-05-16T12:40:56","date_gmt":"2024-05-16T04:40:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/270904.html"},"modified":"2024-05-16T12:41:06","modified_gmt":"2024-05-16T04:41:06","slug":"js-%e7%bc%96%e7%a8%8b%e9%a1%b9%e7%9b%ae%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%81%e8%a3%85%e9%80%89%e9%a1%b9%e5%8d%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/270904.html","title":{"rendered":"js \u7f16\u7a0b\u9879\u76ee\u4e2d\u5982\u4f55\u5c01\u88c5\u9009\u9879\u5361"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24193731\/8ad38bd6-6d1a-4364-9a98-f4cd1ebdb6d9.webp\" alt=\"js \u7f16\u7a0b\u9879\u76ee\u4e2d\u5982\u4f55\u5c01\u88c5\u9009\u9879\u5361\" \/><\/p>\n<p><p>\u9009\u9879\u5361\u529f\u80fd\u662f\u524d\u7aef\u5f00\u53d1\u4e2d\u5e38\u7528\u7684\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u540c\u7684\u89c6\u56fe\u6216\u6587\u6863\u4e4b\u95f4\u8fdb\u884c\u5207\u6362\u3002\u5c01\u88c5\u9009\u9879\u5361\u7684\u6838\u5fc3\u5728\u4e8e\u5c06\u4ee3\u7801\u7ec4\u7ec7\u6210\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u3001\u7ef4\u62a4\u548c\u7ba1\u7406\u7684\u6a21\u5757\u3002<strong>\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u5c01\u88c5\u9009\u9879\u5361\u7ec4\u4ef6<\/strong>\u3001<strong>\u91c7\u7528\u6a21\u5757\u5316\u6280\u672f\u5982ES6\u6a21\u5757\u6216CommonJS<\/strong>\uff0c\u4ee5\u53ca<strong>\u901a\u8fc7\u4e8b\u4ef6\u59d4\u6258\u964d\u4f4e\u4e8b\u4ef6\u76d1\u542c\u5668\u7684\u6570\u91cf<\/strong>\u662f\u5b9e\u73b0\u5c01\u88c5\u7684\u5173\u952e\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u7406\u89e3\u5c01\u88c5\u548c\u7ec4\u4ef6\u5316\u7684\u57fa\u672c\u6982\u5ff5<\/strong><\/p>\n<\/p>\n<p><p>\u5c01\u88c5\u9009\u9879\u5361\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u4ee5\u53ca\u53ef\u8bfb\u6027\u3002\u5728JS\u4e2d\uff0c\u5c01\u88c5\u901a\u5e38\u610f\u5473\u7740\u5c06\u529f\u80fd\u76f8\u5173\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u6253\u5305\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\u6216\u6a21\u5757\u4e2d\u3002\u7ec4\u4ef6\u5316\u5219\u6307\u5728\u5c01\u88c5\u7684\u57fa\u7840\u4e0a\uff0c\u4f7f\u5176\u5177\u6709\u72ec\u7acb\u6027\u548c\u53ef\u63d2\u62d4\u6027\uff0c\u4ece\u800c\u80fd\u591f\u5728\u4e0d\u540c\u9879\u76ee\u6216\u9879\u76ee\u7684\u4e0d\u540c\u90e8\u5206\u4e2d\u91cd\u590d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u9009\u9879\u5361\u7ec4\u4ef6\u901a\u5e38\u5305\u62ec\u4e24\u5927\u90e8\u5206\uff1a\u9009\u9879\u5361\u5934\uff08\u7528\u4e8e\u5207\u6362\u7684\u6309\u94ae\uff09\u548c\u9009\u9879\u5361\u5185\u5bb9\u3002\u5c01\u88c5\u65f6\uff0c\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u4f9b\u4f7f\u7528\u8005\u521b\u5efa\u9009\u9879\u5361\u3001\u76d1\u542c\u5207\u6362\u4e8b\u4ef6\u4ee5\u53ca\u8bbe\u5b9a\u9ed8\u8ba4\u7684\u663e\u793a\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u9009\u9879\u5361\u5c01\u88c5\u7684\u6b65\u9aa4<\/strong><\/p>\n<\/p>\n<p><p><strong>1.\u89c4\u5212HTML\u7ed3\u6784\u548cCSS\u6837\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199JavaScript\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u89c4\u5212\u597d\u9009\u9879\u5361\u7684HTML\u7ed3\u6784\u3002\u6bcf\u4e2a\u9009\u9879\u5361\u90fd\u5e94\u5177\u6709\u4e00\u81f4\u7684\u7ed3\u6784\uff0c\u4fbf\u4e8e\u901a\u8fc7JS\u8fdb\u884c\u64cd\u4f5c\u3002\u540c\u65f6\uff0cCSS\u6837\u5f0f\u4e5f\u5e94\u9884\u5148\u5b9a\u4e49\u597d\uff0c\u5305\u62ec\u9009\u9879\u5361\u7684\u57fa\u672c\u6837\u5f0f\u548c\u6fc0\u6d3b\u72b6\u6001\u65f6\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;div class=&quot;tab-cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ner&quot;&gt;<\/p>\n<p>  &lt;div class=&quot;tab-head&quot;&gt;<\/p>\n<p>    &lt;button class=&quot;tab-button active&quot;&gt;Tab 1&lt;\/button&gt;<\/p>\n<p>    &lt;button class=&quot;tab-button&quot;&gt;Tab 2&lt;\/button&gt;<\/p>\n<p>    &lt;button class=&quot;tab-button&quot;&gt;Tab 3&lt;\/button&gt;<\/p>\n<p>  &lt;\/div&gt;<\/p>\n<p>  &lt;div class=&quot;tab-body&quot;&gt;<\/p>\n<p>    &lt;div class=&quot;tab-content active&quot;&gt;Content 1&lt;\/div&gt;<\/p>\n<p>    &lt;div class=&quot;tab-content&quot;&gt;Content 2&lt;\/div&gt;<\/p>\n<p>    &lt;div class=&quot;tab-content&quot;&gt;Content 3&lt;\/div&gt;<\/p>\n<p>  &lt;\/div&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2.\u5b9e\u73b0\u57fa\u672c\u7684\u9009\u9879\u5361\u5207\u6362\u903b\u8f91<\/strong><\/p>\n<\/p>\n<p><p>\u5c01\u88c5\u7684JavaScript\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u8981\u5b9e\u73b0\u57fa\u672c\u7684\u5207\u6362\u903b\u8f91\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u4e3a\u6bcf\u4e2a\u9009\u9879\u5361\u5934\u6dfb\u52a0\u70b9\u51fb\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u5728\u4e8b\u4ef6\u76d1\u542c\u5668\u4e2d\u5207\u6362\u76f8\u5e94\u7684\u9009\u9879\u5361\u5185\u5bb9\u663e\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">class TabComponent {<\/p>\n<p>  constructor(container) {<\/p>\n<p>    this.container = document.querySelector(container);<\/p>\n<p>    this.init();<\/p>\n<p>  }<\/p>\n<p>  init() {<\/p>\n<p>    this.bindEvents();<\/p>\n<p>  }<\/p>\n<p>  bindEvents() {<\/p>\n<p>    const buttons = this.container.querySelectorAll(&#039;.tab-button&#039;);<\/p>\n<p>    const contents = this.container.querySelectorAll(&#039;.tab-content&#039;);<\/p>\n<p>    buttons.forEach((button, index) =&gt; {<\/p>\n<p>      button.addEventListener(&#039;click&#039;, () =&gt; {<\/p>\n<p>        buttons.forEach(btn =&gt; btn.classList.remove(&#039;active&#039;));<\/p>\n<p>        button.classList.add(&#039;active&#039;);<\/p>\n<p>        contents.forEach(content =&gt; content.classList.remove(&#039;active&#039;));<\/p>\n<p>        contents[index].classList.add(&#039;active&#039;);<\/p>\n<p>      });<\/p>\n<p>    });<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3.\u91c7\u7528\u6a21\u5757\u5316\u5c01\u88c5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3\u5f00\u53d1\u5b9e\u8df5\u4e2d\uff0c\u9009\u62e9\u9002\u5f53\u7684\u6a21\u5757\u5316\u6807\u51c6\uff08ES6 Modules, CommonJS\u7b49\uff09\u6765\u5c01\u88c5\u4ee3\u7801\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u8fd9\u4e0d\u4ec5\u6709\u5229\u4e8e\u4ee3\u7801\u7ec4\u7ec7\uff0c\u8fd8\u4fbf\u4e8e\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u548c\u540e\u7eed\u7684\u4ee3\u7801\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ tabComponent.js<\/p>\n<p>export default class TabComponent {<\/p>\n<p>  \/\/ ...<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u9009\u9879\u5361\u5c01\u88c5\u4e3a\u4e00\u4e2a\u6a21\u5757\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u9879\u76ee\u4e2d\u7684\u4efb\u4f55\u5730\u65b9\u5bfc\u5165\u5e76\u521b\u5efa\u9009\u9879\u5361\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ main.js<\/p>\n<p>import TabComponent from &#039;.\/tabComponent.js&#039;;<\/p>\n<p>const tab = new TabComponent(&#039;.tab-container&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4.\u6269\u5c55\u529f\u80fd<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u7ec4\u4ef6\u7684\u53ef\u7528\u6027\u548c\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u6269\u5c55\u6027\u529f\u80fd\uff0c\u4f8b\u5982\u914d\u7f6e\u662f\u5426\u53ef\u4ee5\u5faa\u73af\u5207\u6362\u3001\u662f\u5426\u81ea\u52a8\u5207\u6362\u3001\u5207\u6362\u901f\u5ea6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">class TabComponent {<\/p>\n<p>  constructor(container, options) {<\/p>\n<p>    this.container = document.querySelector(container);<\/p>\n<p>    this.options = options || {};<\/p>\n<p>    this.init();<\/p>\n<p>  }<\/p>\n<p>  \/\/ ...<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>5.\u63d0\u4f9b\u56de\u8c03\u51fd\u6570<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528JavaScript\u5c01\u88c5\u9009\u9879\u5361\u7684\u529f\u80fd\uff1f<\/strong><\/p>\n<p>\u9009\u9879\u5361\u662fWeb\u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u4ea4\u4e92\u7ec4\u4ef6\uff0c\u4f7f\u7528JavaScript\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u9009\u9879\u5361\u7684\u529f\u80fd\u5c01\u88c5\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cTab\u201d\u6216\u7c7b\u4f3c\u7684\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u5404\u4e2a\u9009\u9879\u5361\u7684\u72b6\u6001\u3001\u6837\u5f0f\u548c\u65b9\u6cd5\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u9009\u9879\u5361\u7684\u5207\u6362\u548c\u4ea4\u4e92\u6548\u679c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528addEventListener\u65b9\u6cd5\u6765\u76d1\u542c\u9009\u9879\u5361\u7684\u70b9\u51fb\u4e8b\u4ef6\uff0c\u5e76\u901a\u8fc7\u4fee\u6539\u6837\u5f0f\u6216\u5185\u5bb9\u6765\u5b9e\u73b0\u9009\u9879\u5361\u7684\u5207\u6362\u6548\u679c\u3002<\/p>\n<p><strong>2. \u6709\u54ea\u4e9b\u5df2\u6709\u7684JavaScript\u5e93\u6216\u6846\u67b6\u53ef\u4ee5\u7528\u4e8e\u5c01\u88c5\u9009\u9879\u5361\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u81ea\u5df1\u624b\u52a8\u5b9e\u73b0\u9009\u9879\u5361\u529f\u80fd\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u4f18\u79c0\u7684JavaScript\u5e93\u6216\u6846\u67b6\u6765\u5c01\u88c5\u9009\u9879\u5361\u3002\u5176\u4e2d\uff0c\u6bd4\u8f83\u5e38\u7528\u7684\u6709jQuery\u3001Vue\u548cReact\u7b49\u3002\u8fd9\u4e9b\u5e93\u6216\u6846\u67b6\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9009\u9879\u5361\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u51cf\u5c11\u6211\u4eec\u7684\u5f00\u53d1\u5de5\u4f5c\u91cf\u3002\u4f8b\u5982\uff0cjQuery\u63d0\u4f9b\u4e86tabs\u65b9\u6cd5\u6765\u5b9e\u73b0\u9009\u9879\u5361\u529f\u80fd\uff0cVue\u548cReact\u53ef\u4ee5\u4f7f\u7528\u5404\u81ea\u7684\u7ec4\u4ef6\u673a\u5236\u6765\u5c01\u88c5\u9009\u9879\u5361\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u63d0\u9ad8\u5c01\u88c5\u9009\u9879\u5361\u7684\u4ee3\u7801\u53ef\u590d\u7528\u6027\u548c\u6027\u80fd\uff1f<\/strong><\/p>\n<p>\u5728\u5c01\u88c5\u9009\u9879\u5361\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u4e00\u4e9b\u63aa\u65bd\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\u548c\u6027\u80fd\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u9009\u9879\u5361\u7684\u6837\u5f0f\u548c\u4ea4\u4e92\u6548\u679c\u4e0e\u4e1a\u52a1\u903b\u8f91\u5206\u79bb\uff0c\u4f7f\u5176\u53d8\u6210\u4e00\u4e2a\u53ef\u914d\u7f6e\u7684\u7ec4\u4ef6\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u591a\u4e2a\u9879\u76ee\u4e2d\u590d\u7528\u8be5\u7ec4\u4ef6\uff0c\u5e76\u901a\u8fc7\u53c2\u6570\u6765\u7075\u6d3b\u914d\u7f6e\u9009\u9879\u5361\u7684\u6837\u5f0f\u548c\u884c\u4e3a\u3002\u5176\u6b21\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u61d2\u52a0\u8f7d\u7b49\u6280\u672f\u6765\u4f18\u5316\u9009\u9879\u5361\u7684\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728\u9009\u9879\u5361\u5185\u5bb9\u8f83\u591a\u6216\u9875\u9762\u590d\u6742\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5ef6\u8fdf\u52a0\u8f7d\u4e0d\u53ef\u89c1\u7684\u9009\u9879\u5361\u5185\u5bb9\uff0c\u63d0\u9ad8\u9875\u9762\u52a0\u8f7d\u901f\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u9009\u9879\u5361\u529f\u80fd\u662f\u524d\u7aef\u5f00\u53d1\u4e2d\u5e38\u7528\u7684\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u540c\u7684\u89c6\u56fe\u6216\u6587\u6863\u4e4b\u95f4\u8fdb\u884c\u5207\u6362\u3002\u5c01\u88c5\u9009\u9879\u5361\u7684\u6838\u5fc3\u5728\u4e8e\u5c06\u4ee3\u7801\u7ec4\u7ec7 [&hellip;]","protected":false},"author":3,"featured_media":270919,"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\/270904"}],"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=270904"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/270904\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/270919"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=270904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=270904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=270904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}