3 回答

TA貢獻1911條經(jīng)驗 獲得超7個贊
你太復(fù)雜了:)你正確地設(shè)置了你的點擊監(jiān)聽器,但你只需要在監(jiān)聽器中執(zhí)行 2 個簡單的步驟:
1. 重置當(dāng)前活動按鈕:只需獲取所有具有活動類的按鈕(我們不關(guān)心其余的,所以不需要處理它們?。┎h除它:
document.querySelectorAll(".portfolio-buttons-active").forEach((button) => {
button.classList.remove("portfolio-buttons-active");
});
2. 將單擊的按鈕設(shè)置為活動:您的單擊處理程序已添加到按鈕,因此我們在單擊該按鈕時向其添加活動類:
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ù)中,以防您以后需要在每個步驟中添加更多功能
/* 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貢獻1829條經(jīng)驗 獲得超13個贊
.您在querySelectorAll選擇按鈕的地方缺少一個。所以它正在尋找元素portfolio-buttons而不是類。
該classList接口有一個contains方法來檢查元素上是否存在類。所以不需要循環(huán)檢查字符串className。也根本沒有必要,如果類不在元素上,則不會刪除任何內(nèi)容。
對于重置按鈕,給它一個方法來識別它的唯一性。在上面的示例中,我給了它一個value帶有字符串的屬性。單擊時檢查是否單擊了重置按鈕,并且不要添加新的活動類。
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貢獻1946條經(jīng)驗 獲得超4個贊
您可以通過檢查來簡化它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)前元素,則刪除活動類,否則,添加活動類。
添加回答
舉報