<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>前端設計 &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/%e5%89%8d%e7%ab%af%e8%a8%ad%e8%a8%88/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Fri, 12 Dec 2025 14:44:42 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://piglife.tw/wp-content/uploads/2017/10/cropped-logo-1-32x32.png</url>
	<title>前端設計 &#8211; 小豬日常</title>
	<link>https://piglife.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>原生 JavaScript 實作 Tab 功能選單筆記</title>
		<link>https://piglife.tw/technical-notes/native-javascript-tab-menu/</link>
					<comments>https://piglife.tw/technical-notes/native-javascript-tab-menu/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Thu, 02 Nov 2017 17:06:42 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[DOM操作]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[tab功能]]></category>
		<category><![CDATA[事件處理]]></category>
		<category><![CDATA[前端設計]]></category>
		<category><![CDATA[原生js]]></category>
		<guid isPermaLink="false">https://piglife.tw/?p=237</guid>

					<description><![CDATA[介紹如何使用原生 JavaScript 實作 Tab 功能選單，說明 HTML 結構與 JavaSc...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>Tab 功能是前端常見的 UI 元件，許多框架都提供現成的實作，但若想深入理解其背後的運作原理，使用原生 JavaScript 自行實作是一個很好的練習。本文適合有基礎程式能力的工程師或自學者，透過範例拆解，了解如何控制元素顯示與切換樣式。</p>
<h2 class="wp-block-heading">HTML 結構說明</h2>
<p>Tab 功能主要由兩部分組成：控制按鈕與內容區塊。</p>
<ul>
<li>按鈕部分使用 <code> &lt;a&gt;</code> 標籤，<code>href=&quot;javascript:void(0)&quot;</code> 用來阻止預設跳轉行為。</li>
<li>按鈕會帶有 <code>tablink</code> 及特定的 <code>testbtn</code> class，方便 JavaScript 操作。</li>
<li><code>onclick</code> 事件會呼叫 <code>openClass(event, &#039;classX&#039;)</code>，帶入事件物件與對應內容區塊的 id。</li>
<li>內容區塊以 <code>id</code> 和 <code>class=&quot;class&quot;</code> 標示，對應按鈕的參數，方便顯示與隱藏。</li>
</ul>
<pre><code class="lang-html language-html html">&lt;div class=&quot;bar-block&quot;&gt;
  &lt;div class=&quot;container&quot;&gt;

&lt;h5&gt;選單&lt;/h5&gt;
    &lt;a href=&quot;javascript:void(0)&quot; class=&quot;bar-item button tablink testbtn&quot; onclick=&quot;openClass(event, &#039;class1&#039;)&quot;&gt;教學1&lt;/a&gt;
    &lt;a href=&quot;javascript:void(0)&quot; class=&quot;bar-item button tablink&quot; onclick=&quot;openClass(event, &#039;class2&#039;)&quot;&gt;教學2&lt;/a&gt;
    &lt;a href=&quot;javascript:void(0)&quot; class=&quot;bar-item button tablink&quot; onclick=&quot;openClass(event, &#039;class3&#039;)&quot;&gt;教學3&lt;/a&gt;
    &lt;a href=&quot;javascript:void(0)&quot; class=&quot;bar-item button tablink&quot; onclick=&quot;openClass(event, &#039;class4&#039;)&quot;&gt;教學4&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div id=&quot;class1&quot; class=&quot;container class&quot;&gt;
  教學1內容
&lt;/div&gt;
&lt;div id=&quot;class2&quot; class=&quot;container class&quot;&gt;
  教學2內容
&lt;/div&gt;
&lt;div id=&quot;class3&quot; class=&quot;container class&quot;&gt;
  教學3內容
&lt;/div&gt;
&lt;div id=&quot;class4&quot; class=&quot;container class&quot;&gt;
  教學4內容
&lt;/div&gt;</code></pre>
<h2 class="wp-block-heading">JavaScript 功能解析</h2>
<p><code>openClass</code> 函式負責切換 Tab 的顯示狀態與按鈕樣式：</p>
<ol>
<li>使用 <code>document.getElementsByClassName(&quot;class&quot;)</code> 取得所有內容區塊，並將它們隱藏（<code>display = &quot;none&quot;</code>）。</li>
<li>取得所有按鈕元素 <code>tablinks</code>，並移除所有按鈕的 <code>red</code> 樣式，確保只有一個按鈕被標示為選中。</li>
<li>根據傳入的 <code>className</code> 參數，將對應內容區塊顯示（<code>display = &quot;block&quot;</code>）。</li>
<li>對當前點擊的按鈕元素新增 <code>red</code> class，標示為選中狀態。</li>
</ol>
<pre><code class="lang-js language-js js">function openClass(evt, className) {
  var i, x, tablinks;
  x = document.getElementsByClassName(&quot;class&quot;);
  for (i = 0; i &lt; x.length; i++) {
    x[i].style.display = &quot;none&quot;;
  }
  tablinks = document.getElementsByClassName(&quot;tablink&quot;);
  for (i = 0; i &lt; tablinks.length; i++) {
    tablinks[i].classList.remove(&quot;red&quot;);
  }
  document.getElementById(className).style.display = &quot;block&quot;;
  evt.currentTarget.classList.add(&quot;red&quot;);
}

// 頁面載入後自動觸發第一個帶有 testbtn 的按鈕點擊
var mybtn = document.getElementsByClassName(&quot;testbtn&quot;)[0];
mybtn.click();</code></pre>
<h2 class="wp-block-heading">實際應用與延伸</h2>
<ul>
<li>可依需求調整 <code>red</code> class 的 CSS 樣式，達到不同的選中效果。</li>
<li>若 Tab 數量動態產生，需確保事件綁定正確。</li>
<li>可改用事件代理方式優化效能。</li>
<li>進階可結合動畫效果，提升使用者體驗。</li>
</ul>
<h2 class="wp-block-heading">常見坑點</h2>
<ul>
<li>忘記阻止 <code> &lt;a&gt;</code> 的預設行為會導致頁面跳轉。</li>
<li>未正確移除其他按鈕的選中樣式，導致多個 Tab 同時被標示。</li>
<li>忘記預設觸發第一個 Tab，頁面初始狀態會空白。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-text language-text text">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;zh-TW&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;原生 JavaScript Tab 功能&lt;/title&gt;
&lt;style&gt;
  .red { color: red; font-weight: bold; }
  .class { display: none; }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div class=&quot;bar-block&quot;&gt;
  &lt;div class=&quot;container&quot;&gt;

&lt;h5&gt;選單&lt;/h5&gt;
    &lt;a href=&quot;javascript:void(0)&quot; class=&quot;bar-item button tablink testbtn&quot; onclick=&quot;openClass(event, &#039;class1&#039;)&quot;&gt;教學1&lt;/a&gt;
    &lt;a href=&quot;javascript:void(0)&quot; class=&quot;bar-item button tablink&quot; onclick=&quot;openClass(event, &#039;class2&#039;)&quot;&gt;教學2&lt;/a&gt;
    &lt;a href=&quot;javascript:void(0)&quot; class=&quot;bar-item button tablink&quot; onclick=&quot;openClass(event, &#039;class3&#039;)&quot;&gt;教學3&lt;/a&gt;
    &lt;a href=&quot;javascript:void(0)&quot; class=&quot;bar-item button tablink&quot; onclick=&quot;openClass(event, &#039;class4&#039;)&quot;&gt;教學4&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div id=&quot;class1&quot; class=&quot;container class&quot;&gt;
  教學1內容
&lt;/div&gt;
&lt;div id=&quot;class2&quot; class=&quot;container class&quot;&gt;
  教學2內容
&lt;/div&gt;
&lt;div id=&quot;class3&quot; class=&quot;container class&quot;&gt;
  教學3內容
&lt;/div&gt;
&lt;div id=&quot;class4&quot; class=&quot;container class&quot;&gt;
  教學4內容
&lt;/div&gt;

&lt;script&gt;
function openClass(evt, className) {
  var i, x, tablinks;
  x = document.getElementsByClassName(&quot;class&quot;);
  for (i = 0; i &lt; x.length; i++) {
    x[i].style.display = &quot;none&quot;;
  }
  tablinks = document.getElementsByClassName(&quot;tablink&quot;);
  for (i = 0; i &lt; tablinks.length; i++) {
    tablinks[i].classList.remove(&quot;red&quot;);
  }
  document.getElementById(className).style.display = &quot;block&quot;;
  evt.currentTarget.classList.add(&quot;red&quot;);
}

var mybtn = document.getElementsByClassName(&quot;testbtn&quot;)[0];
mybtn.click();
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/native-javascript-tab-menu/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
