css 分類(lèi)選項(xiàng)卡可以顯示分類(lèi)內(nèi)容,可通過(guò)以下步驟實(shí)現(xiàn):創(chuàng)建 html 容器,包含選項(xiàng)卡按鈕和內(nèi)容,并設(shè)置其 id 屬性;設(shè)置選項(xiàng)卡和內(nèi)容的 css 樣式;使用 javascript 監(jiān)聽(tīng)選項(xiàng)卡按鈕點(diǎn)擊事件,隱藏所有選項(xiàng)卡內(nèi)容并顯示與所單擊按鈕相對(duì)應(yīng)的選項(xiàng)卡內(nèi)容。
CSS 分類(lèi)選項(xiàng)卡打開(kāi)方法
CSS 分類(lèi)選項(xiàng)卡是一種用于組織和展示內(nèi)容的方式,它允許用戶(hù)通過(guò)單擊選項(xiàng)卡來(lái)切換不同類(lèi)別的內(nèi)容。要打開(kāi) CSS 分類(lèi)選項(xiàng)卡,需要遵循以下步驟:
HTML 結(jié)構(gòu)
-
在 HTML 中創(chuàng)建包含選項(xiàng)卡欄和選項(xiàng)卡內(nèi)容的容器元素。
為每個(gè)選項(xiàng)卡創(chuàng)建
<button></button>
或 <a></a>
元素,并設(shè)置其 id
屬性。為每個(gè)選項(xiàng)卡內(nèi)容創(chuàng)建一個(gè)
<div> 元素,并設(shè)置其 <code>id
屬性。
以下是示例 HTML 結(jié)構(gòu):
<code class="html"><div class="tabs"> <button id="tab1">選項(xiàng)卡 1</button> <button id="tab2">選項(xiàng)卡 2</button> <div id="content1">內(nèi)容 1</div> <div id="content2">內(nèi)容 2</div> </div></code>
登錄后復(fù)制
CSS 樣式
-
設(shè)置選項(xiàng)卡欄的樣式,包括背景顏色、字體大小和對(duì)齊方式。
設(shè)置選項(xiàng)卡按鈕的樣式,包括未激活和激活狀態(tài)的樣式。
設(shè)置選項(xiàng)卡內(nèi)容的樣式,包括背景顏色、填充和邊框。
以下是示例 CSS 樣式:
<code class="<a style='color:#f60; text-decoration:underline;' href=" https: target="_blank">css">.tabs { display: flex; background-color: #f1f1f1; padding: 10px; } .tabs button { padding: 10px 20px; background-color: #ccc; border: none; cursor: pointer; } .tabs button.active { background-color: #f1f1f1; } .tabs div { display: none; padding: 20px; } .tabs div.active { display: block; }</code>
登錄后復(fù)制
JavaScript 事件處理
-
使用 JavaScript 監(jiān)聽(tīng)選項(xiàng)卡按鈕的點(diǎn)擊事件。
當(dāng)用戶(hù)單擊按鈕時(shí),隱藏所有選項(xiàng)卡內(nèi)容。
顯示與所單擊按鈕相對(duì)應(yīng)的選項(xiàng)卡內(nèi)容。
以下是示例 JavaScript 事件處理:
<code class="javascript">const tabs = document.querySelector('.tabs'); tabs.addEventListener('click', (e) => { if (e.target.tagName === 'BUTTON') { const tabId = e.target.id; const contentId = tabId.replace('tab', 'content'); const buttons = tabs.querySelectorAll('button'); buttons.forEach(btn => btn.classList.remove('active')); e.target.classList.add('active'); const contents = tabs.querySelectorAll('div'); contents.forEach(content => content.classList.remove('active')); const content = tabs.querySelector(`#${contentId}`); content.classList.add('active'); } });</code>
登錄后復(fù)制