{"id":297131,"date":"2024-05-20T16:19:06","date_gmt":"2024-05-20T08:19:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/297131.html"},"modified":"2024-05-20T16:19:18","modified_gmt":"2024-05-20T08:19:18","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-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/297131.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\/27144753\/030b963b-2405-4e4f-a223-eda78f7e7478.webp\" alt=\"js \u7f16\u7a0b\u9879\u76ee\u4e2d\u5982\u4f55\u5c01\u88c5\u9009\u9879\u5361\" \/><\/p>\n<p><p>JavaScript \u7f16\u7a0b\u9879\u76ee\u4e2d\u5c01\u88c5\u9009\u9879\u5361\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u70b9\uff1a<strong>\u5b9a\u4e49\u901a\u7528\u63a5\u53e3\u3001\u62bd\u8c61\u5171\u7528\u903b\u8f91\u3001\u63d0\u4f9b\u5b9a\u5236\u5316\u914d\u7f6e<\/strong>\u3001<strong>\u4ee5\u53ca\u4fdd\u6301\u6613\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/strong>\u3002\u5c01\u88c5\u9009\u9879\u5361\u7684\u5173\u952e\u6b65\u9aa4\u662f\u521b\u5efa\u4e00\u4e2a\u9009\u9879\u5361\u7ec4\u4ef6\uff0c\u8be5\u7ec4\u4ef6\u53ef\u4ee5\u901a\u8fc7\u63a5\u6536\u4e00\u4e9b\u53c2\u6570\u548c\u914d\u7f6e\u6765\u786e\u5b9a\u5176\u529f\u80fd\u548c\u6837\u5f0f\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u6216\u4e00\u4e2a\u7c7b\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u5176\u79f0\u4e4b\u4e3a<code>Tab<\/code>\u3002\u5b83\u8d1f\u8d23\u63a5\u6536\u914d\u7f6e\u5bf9\u8c61\u548c\u8981\u7ed1\u5b9a\u7684DOM\u5143\u7d20\u3002<strong>\u6784\u9020\u51fd\u6570\u5c06\u914d\u7f6e\u53c2\u6570\u548c\u5143\u7d20\u4fdd\u5b58\u5728\u5b9e\u4f8b\u5c5e\u6027\u4e2d<\/strong>\uff0c\u5e76\u8c03\u7528\u521d\u59cb\u5316\u65b9\u6cd5\u6765\u8bbe\u7f6e\u4e8b\u4ef6\u76d1\u542c\u5668\u548c\u9ed8\u8ba4\u6fc0\u6d3b\u7684\u9009\u9879\u5361\u3002\u91cd\u8981\u7684\u662f\uff0c\u4e00\u5b9a\u8981\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684API\u6765\u5141\u8bb8\u7528\u6237\u5207\u6362\u9009\u9879\u3001\u6dfb\u52a0\u6216\u5220\u9664\u9009\u9879\u5361\u3001\u5e76\u5728\u9009\u9879\u6539\u53d8\u65f6\u6709\u56de\u8c03\u51fd\u6570\u3002\u5728\u5c01\u88c5\u7684\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u4f60\u7684\u4ee3\u7801\u662f\u53ef\u91cd\u7528\u7684\uff0c\u8fd9\u610f\u5473\u7740\u76f8\u540c\u7684\u903b\u8f91\u53ef\u4ee5\u5e94\u7528\u4e8e\u9879\u76ee\u4e2d\u7684\u4efb\u4f55\u90e8\u5206\uff0c\u800c\u65e0\u9700\u91cd\u5199\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u6784\u9020\u51fd\u6570\u6216\u7c7b<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u6216\u7c7b<code>Tab<\/code>\uff0c\u5b83\u8d1f\u8d23\u88c5\u8f7d\u9009\u9879\u5361\u7684\u903b\u8f91\u548c\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-js\">function Tab(options) {<\/p>\n<p>    \/\/ \u4fdd\u5b58\u914d\u7f6e\u53c2\u6570<\/p>\n<p>    this.options = options || {}; <\/p>\n<p>    \/\/ \u4fdd\u5b58\u7ed1\u5b9a\u7684\u5143\u7d20<\/p>\n<p>    this.element = this.options.element; <\/p>\n<p>    \/\/ \u521d\u59cb\u5316<\/p>\n<p>    this.init(); <\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u63d0\u4f9b\u521d\u59cb\u5316\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u7c7b\u5185\u90e8\u63d0\u4f9b<code>init<\/code>\u65b9\u6cd5\u7528\u6765\u521d\u59cb\u5316\u9009\u9879\u5361\u7684\u72b6\u6001\u548c\u4e8b\u4ef6\u7ed1\u5b9a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-js\">Tab.prototype.init = function() {<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u4e8b\u4ef6\u76d1\u542c<\/p>\n<p>    this.setupListeners(); <\/p>\n<p>    \/\/ \u8bbe\u7f6e\u9ed8\u8ba4\u9009\u9879\u5361\u6fc0\u6d3b\u72b6\u6001<\/p>\n<p>    this.activate(this.options.defaultIndex || 0); <\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u7f6e\u4e8b\u4ef6\u76d1\u542c<\/h3>\n<\/p>\n<p><p><code>setupListeners<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u9009\u9879\u5361\u6807\u9898\u4e0a\u8bbe\u7f6e\u70b9\u51fb\u4e8b\u4ef6\uff0c\u4ece\u800c\u5207\u6362\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-js\">Tab.prototype.setupListeners = function() {<\/p>\n<p>    var titles = this.element.querySelectorAll(&#039;.tab-title&#039;);<\/p>\n<p>    titles.forEach((title, index) =&gt; {<\/p>\n<p>        title.addEventListener(&#039;click&#039;, () =&gt; {<\/p>\n<p>            this.activate(index);<\/p>\n<p>        });<\/p>\n<p>    });<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6fc0\u6d3b\u9009\u9879\u5361<\/h3>\n<\/p>\n<p><p><code>activate<\/code>\u65b9\u6cd5\u7528\u4e8e\u6fc0\u6d3b\u9009\u9879\u5361\uff0c\u5e76\u663e\u793a\u76f8\u5173\u5185\u5bb9\uff0c\u540c\u65f6\u9690\u85cf\u5176\u4ed6\u9009\u9879\u5361\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-js\">Tab.prototype.activate = function(index) {<\/p>\n<p>    \/\/ \u5148\u5c06\u6240\u6709\u5185\u5bb9\u9690\u85cf<\/p>\n<p>    var contents = this.element.querySelectorAll(&#039;.tab-content&#039;);<\/p>\n<p>    contents.forEach((content) =&gt; {<\/p>\n<p>        content.style.display = &#039;none&#039;;<\/p>\n<p>    });<\/p>\n<p>    \/\/ \u5c06\u70b9\u51fb\u7684\u9009\u9879\u5361\u5185\u5bb9\u663e\u793a<\/p>\n<p>    contents[index].style.display = &#039;block&#039;;<\/p>\n<p>    \/\/ \u66f4\u65b0\u6d3b\u52a8\u72b6\u6001<\/p>\n<p>    this.updateActiveStatus(index);<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u66f4\u65b0\u6d3b\u52a8\u72b6\u6001<\/h3>\n<\/p>\n<p><p><code>updateActiveStatus<\/code>\u65b9\u6cd5\u7528\u4e8e\u66f4\u65b0\u9009\u9879\u5361\u7684\u6d3b\u8dc3\u72b6\u6001\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u6539\u53d8\u6fc0\u6d3b\u9009\u9879\u5361\u6807\u9898\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-js\">Tab.prototype.updateActiveStatus = function(activeIndex) {<\/p>\n<p>    var titles = this.element.querySelectorAll(&#039;.tab-title&#039;);<\/p>\n<p>    titles.forEach((title, index) =&gt; {<\/p>\n<p>        if (index === activeIndex) {<\/p>\n<p>            title.classList.add(&#039;active&#039;);<\/p>\n<p>        } else {<\/p>\n<p>            title.classList.remove(&#039;active&#039;);<\/p>\n<p>        }<\/p>\n<p>    });<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u786e\u4fdd\u53ef\u914d\u7f6e\u6027<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u9009\u9879\u5361\u7ec4\u4ef6\u6613\u7528\u4e14\u7075\u6d3b\uff0c\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u4e00\u5b9a\u7a0b\u5ea6\u7684\u914d\u7f6e\u6027\uff0c\u6bd4\u5982\u9ed8\u8ba4\u6fc0\u6d3b\u7684\u9009\u9879\u5361\u7d22\u5f15\u3001\u5404\u79cd\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-js\">var myTab = new Tab({<\/p>\n<p>    element: document.getElementById(&#039;myTab&#039;),<\/p>\n<p>    defaultIndex: 2,<\/p>\n<p>    onTabChange: function(activeIndex) {<\/p>\n<p>        console.log(&#039;Tab changed to: &#039; + activeIndex);<\/p>\n<p>    }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u52a8\u6001\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9009\u9879\u5361\u7684\u5185\u5bb9\u53ef\u80fd\u4f1a\u52a8\u6001\u52a0\u8f7d\u6216\u66f4\u65b0\u3002\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u65b9\u6cd5\u6765\u5904\u7406\u5185\u5bb9\u7684\u52a8\u6001\u6dfb\u52a0\u6216\u79fb\u9664\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-js\">Tab.prototype.addTab = function(title, content) {<\/p>\n<p>    \/\/ \u6dfb\u52a0\u65b0\u7684\u9009\u9879\u5361\u6807\u9898\u548c\u5185\u5bb9<\/p>\n<p>    \/\/ ...<\/p>\n<p>    this.updateListeners();<\/p>\n<p>};<\/p>\n<p>Tab.prototype.removeTab = function(index) {<\/p>\n<p>    \/\/ \u79fb\u9664\u6307\u5b9a\u7d22\u5f15\u7684\u9009\u9879\u5361\u6807\u9898\u548c\u5185\u5bb9<\/p>\n<p>    \/\/ ...<\/p>\n<p>    this.updateListeners();<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u7efc\u5408\u8003\u8651\u53ef\u7ef4\u62a4\u6027\u548c\u62d3\u5c55\u6027<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5f53\u4f60\u7684\u5c01\u88c5\u903b\u8f91\u53d8\u5f97\u66f4\u590d\u6742\u65f6\uff0c\u59cb\u7ec8\u8981\u8003\u8651\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u62d3\u5c55\u6027\u3002\u53ef\u80fd\u4f1a\u5f15\u5165\u6a21\u5757\u5316\u548c\u5de5\u7a0b\u5316\u5de5\u5177\uff0c\u6bd4\u5982\u4f7f\u7528ES6\u6a21\u5757\u6216Webpack\u6765\u7ec4\u7ec7\u4f60\u7684\u4ee3\u7801\uff0c\u6216\u8005\u8f6c\u5411\u73b0\u4ee3\u524d\u7aef\u6846\u67b6\u5982React\u3001Vue\u7b49\uff0c\u4ee5\u7ec4\u4ef6\u7684\u5f62\u5f0f\u5c01\u88c5\u9009\u9879\u5361\u903b\u8f91\u3002\u6b64\u5916\uff0c\u4fdd\u6301\u826f\u597d\u7684\u6ce8\u91ca\u548c\u6587\u6863\uff0c\u5bf9\u4e8e\u4ed6\u4eba\u7406\u89e3\u4f60\u7684\u5c01\u88c5\u6765\u8bf4\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7075\u6d3b\u3001\u53ef\u590d\u7528\u4e14\u6613\u4e8e\u7ef4\u62a4\u7684\u9009\u9879\u5361\u7ec4\u4ef6\uff0c\u63d0\u9ad8\u4f60\u7684JavaScript\u9879\u76ee\u7684\u8d28\u91cf\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728JavaScript\u7f16\u7a0b\u9879\u76ee\u4e2d\u5b9e\u73b0\u9009\u9879\u5361\u7684\u5c01\u88c5\uff1f<\/strong><\/p>\n<p>\u9009\u9879\u5361\u7684\u5c01\u88c5\u5728JavaScript\u7f16\u7a0b\u9879\u76ee\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u901a\u8fc7\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u8fdb\u884c\u5c01\u88c5<\/strong>\uff1a\u4f7f\u7528JavaScript\u7684\u7c7b\u548c\u5bf9\u8c61\u6765\u5c01\u88c5\u9009\u9879\u5361\u529f\u80fd\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u7ef4\u62a4\u3002\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aTab\u7c7b\uff0c\u7c7b\u4e2d\u5305\u542b\u9009\u9879\u5361\u7684\u521d\u59cb\u5316\u3001\u5207\u6362\u548c\u4e8b\u4ef6\u7ed1\u5b9a\u7b49\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u6a21\u5757\u5316\u7684\u65b9\u5f0f\u8fdb\u884c\u5c01\u88c5<\/strong>\uff1a\u4f7f\u7528ES6\u7684\u6a21\u5757\u5316\u8bed\u6cd5\uff08\u5982import\u548cexport\uff09\u6216\u8005\u5176\u4ed6\u6a21\u5757\u5316\u65b9\u6848\uff08\u5982CommonJS\u6216AMD\uff09\u6765\u5b9e\u73b0\u9009\u9879\u5361\u7684\u5c01\u88c5\u3002\u5c06\u9009\u9879\u5361\u76f8\u5173\u7684\u4ee3\u7801\u5c01\u88c5\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u6a21\u5757\u4e2d\uff0c\u7136\u540e\u5728\u9879\u76ee\u4e2d\u5f15\u5165\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5229\u7528\u63d2\u4ef6\u6216\u6846\u67b6\u8fdb\u884c\u5c01\u88c5<\/strong>\uff1a\u6709\u5f88\u591a\u5f00\u6e90\u7684JavaScript\u63d2\u4ef6\u6216\u6846\u67b6\u53ef\u4ee5\u7528\u6765\u5c01\u88c5\u9009\u9879\u5361\uff0c\u5982jQuery UI\u3001Bootstrap\u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u63d2\u4ef6\u6216\u6846\u67b6\uff0c\u53ef\u4ee5\u5feb\u901f\u5730\u5b9e\u73b0\u9009\u9879\u5361\u529f\u80fd\uff0c\u5e76\u4e14\u5177\u6709\u4e00\u4e9b\u989d\u5916\u7684\u7279\u6027\u548c\u6837\u5f0f\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u8fdb\u884c\u5c01\u88c5<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528JavaScript\u7684\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u6765\u5c01\u88c5\u9009\u9879\u5361\u529f\u80fd\u3002\u901a\u8fc7\u5b9a\u4e49\u9009\u9879\u5361\u5207\u6362\u4e8b\u4ef6\uff0c\u5e76\u5728\u9700\u8981\u7684\u5730\u65b9\u89e6\u53d1\u8be5\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9009\u9879\u5361\u7684\u5207\u6362\u548c\u5bf9\u5e94\u5185\u5bb9\u7684\u5237\u65b0\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u5c01\u88c5\u65b9\u5f0f\uff0c\u90fd\u9700\u8981\u8003\u8651\u9009\u9879\u5361\u7684\u521d\u59cb\u5316\u548c\u914d\u7f6e\u3001\u5207\u6362\u6548\u679c\u3001\u4e8b\u4ef6\u7ed1\u5b9a\u4ee5\u53ca\u5bf9\u5916\u66b4\u9732\u7684\u63a5\u53e3\u7b49\u65b9\u9762\u3002\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u548c\u81ea\u8eab\u6280\u672f\u6c34\u5e73\u9009\u62e9\u5408\u9002\u7684\u5c01\u88c5\u65b9\u5f0f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript \u7f16\u7a0b\u9879\u76ee\u4e2d\u5c01\u88c5\u9009\u9879\u5361\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u70b9\uff1a\u5b9a\u4e49\u901a\u7528\u63a5\u53e3\u3001\u62bd\u8c61\u5171\u7528\u903b\u8f91\u3001\u63d0\u4f9b\u5b9a\u5236\u5316\u914d\u7f6e\u3001 [&hellip;]","protected":false},"author":3,"featured_media":297145,"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\/297131"}],"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=297131"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/297131\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/297145"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=297131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=297131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=297131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}