3 回答

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊
你太復(fù)雜了:)你正確地設(shè)置了你的點(diǎn)擊監(jiān)聽(tīng)器,但你只需要在監(jiān)聽(tīng)器中執(zhí)行 2 個(gè)簡(jiǎn)單的步驟:
1. 重置當(dāng)前活動(dòng)按鈕:只需獲取所有具有活動(dòng)類的按鈕(我們不關(guān)心其余的,所以不需要處理它們?。┎h除它:
document.querySelectorAll(".portfolio-buttons-active").forEach((button) => {
button.classList.remove("portfolio-buttons-active");
});
2. 將單擊的按鈕設(shè)置為活動(dòng):您的單擊處理程序已添加到按鈕,因此我們?cè)趩螕粼摪粹o時(shí)向其添加活動(dòng)類:
button.classList.add("portfolio-buttons-active");
就是這樣!將這些放在一起,您需要的是以下內(nèi)容:
document.querySelectorAll(".portfolio-buttons").forEach((button) => {
button.addEventListener("click", function() {
// Reset the currently active buttons:
document.querySelectorAll(".portfolio-buttons-active").forEach((button) => {
button.classList.remove("portfolio-buttons-active");
});
// Add the active class to the clicked button
button.classList.add("portfolio-buttons-active");
});
});
工作示例:我已將步驟 1 和 2 放入此代碼中的函數(shù)中,以防您以后需要在每個(gè)步驟中添加更多功能
/* Add the active class to the button passed in */
function setThisButtonActive(button) {
button.classList.add("portfolio-buttons-active");
}
/* select all active buttons, and remove the active class from them */
function resetActiveButton() {
document.querySelectorAll(".portfolio-buttons-active").forEach((button) => {
button.classList.remove("portfolio-buttons-active");
});
}
document.querySelectorAll(".portfolio-buttons").forEach((button) => {
button.addEventListener("click", function() {
resetActiveButton();
setThisButtonActive(button);
});
});
/* Style the buttons */
.portfolio-buttons {
border: none;
outline: none;
padding: 12px 16px;
/* background-color: white; */
cursor: pointer;
}
.portfolio-buttons-normal {
background-color: white;
}
.portfolio-buttons:hover {
background-color: #ddd;
}
.portfolio-buttons-active {
background-color: #666;
color: white;
}
<div id="myBtnContainer">
<button class="portfolio-buttons portfolio-buttons-normal portfolio-buttons-active">Show
all</button>
<button class="portfolio-buttons portfolio-buttons-normal">.html</button>
<button class="portfolio-buttons portfolio-buttons-normal">.css</button>
<button class="portfolio-buttons portfolio-buttons-normal">.js</button>
<button class="portfolio-buttons portfolio-buttons-normal">.java</button>
<button class="portfolio-buttons portfolio-buttons-normal">.py</button>
</div>

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
.您在querySelectorAll選擇按鈕的地方缺少一個(gè)。所以它正在尋找元素portfolio-buttons而不是類。
該classList接口有一個(gè)contains方法來(lái)檢查元素上是否存在類。所以不需要循環(huán)檢查字符串className。也根本沒(méi)有必要,如果類不在元素上,則不會(huì)刪除任何內(nèi)容。
對(duì)于重置按鈕,給它一個(gè)方法來(lái)識(shí)別它的唯一性。在上面的示例中,我給了它一個(gè)value帶有字符串的屬性。單擊時(shí)檢查是否單擊了重置按鈕,并且不要添加新的活動(dòng)類。
const buttons = document.querySelectorAll(".portfolio-buttons");
buttons.forEach((button) => {
button.addEventListener('click', event => {
buttons.forEach(button => button.classList.remove('portfolio-buttons-active'));
if (button.value !== 'reset') {
button.classList.add('portfolio-buttons-active');
}
});
});
/* Style the buttons */
.portfolio-buttons {
border: none;
outline: none;
padding: 12px 16px;
/* background-color: white; */
cursor: pointer;
}
.portfolio-buttons-normal {
background-color: white;
}
.portfolio-buttons:hover {
background-color: #ddd;
}
.portfolio-buttons-active {
background-color: #666;
color: white;
}
<div id="myBtnContainer">
<button class="portfolio-buttons portfolio-buttons-normal portfolio-buttons-active" value="reset">Show all</button>
<button class="portfolio-buttons portfolio-buttons-normal">.html</button>
<button class="portfolio-buttons portfolio-buttons-normal">.css</button>
<button class="portfolio-buttons portfolio-buttons-normal">.js</button>
<button class="portfolio-buttons portfolio-buttons-normal">.java</button>
<button class="portfolio-buttons portfolio-buttons-normal">.py</button>
</div>

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以通過(guò)檢查來(lái)簡(jiǎn)化它e.target
:
const buttons = document.querySelectorAll(".portfolio-buttons");
buttons.forEach(button => {
button.addEventListener("click", function(e) {
e.target.classList.add('portfolio-buttons-active');
buttons.forEach(item => {
if (item !== e.target) {
item.classList.remove('portfolio-buttons-active');
}
});
});
});
.portfolio-buttons {
border: none;
outline: none;
padding: 12px 16px;
/* background-color: white; */
cursor: pointer;
}
.portfolio-buttons-normal {
background-color: white;
}
.portfolio-buttons:hover {
background-color: #ddd;
}
.portfolio-buttons-active {
background-color: #666;
color: white;
}
<div id="myBtnContainer">
<button class="portfolio-buttons portfolio-buttons-normal portfolio-buttons-active">Show
all</button>
<button class="portfolio-buttons portfolio-buttons-normal">.html</button>
<button class="portfolio-buttons portfolio-buttons-normal">.css</button>
<button class="portfolio-buttons portfolio-buttons-normal">.js</button>
<button class="portfolio-buttons portfolio-buttons-normal">.java</button>
<button class="portfolio-buttons portfolio-buttons-normal">.py</button>
</div>
如果e.target
不是循環(huán)中的當(dāng)前元素,則刪除活動(dòng)類,否則,添加活動(dòng)類。
添加回答
舉報(bào)