2 回答
TA贡献1827条经验 获得超4个赞
这很粗糙,因为我没有完整的代码,但大致如下:
const allDivs = Array.from(document.querySelectorAll('div'))
const mainHeader = document.querySelector('header')
function clearAnimations() {
[maleCharacters, femaleCharacters, otherCharacters].forEach(c =>
c.forEach(character => allDivs.find((oneDiv) =>
oneDiv.firstChild.textContent === character.name).className = '';
});
}
let maleButton = document.createElement('button')
maleButton.textContent = 'Male Characters'
maleButton.addEventListener('click', () => {
clearAnimations();
maleCharacters.forEach(character => {
let matchedDiv = allDivs.find((oneDiv) => {
return oneDiv.firstChild.textContent === character.name
})
//matchedDiv.setAttribute("style", "display: none;")
matchedDiv.className = 'animated infinite bounce delay-2s'
})
});
let femaleButton = document.createElement('button')
femaleButton.textContent = 'Female Characters'
femaleButton.addEventListener('click', () => {
clearAnimations();
femaleCharacters.forEach(character => {
let matchedDiv = allDivs.find((oneDiv) => {
return oneDiv.firstChild.textContent === character.name
})
//matchedDiv.setAttribute("style", "display: none;")
matchedDiv.className = 'animated infinite bounce delay-2s;'
})
});
let otherButton = document.createElement('button')
otherButton.textContent = 'Non-Gender Characters'
otherButton.addEventListener('click', () => {
clearAnimations();
otherCharacters.forEach(character => {
let matchedDiv = allDivs.find((oneDiv) => {
return oneDiv.firstChild.textContent === character.name
})
//matchedDiv.setAttribute("style", "display: none;")
matchedDiv.className = 'animated infinite bounce delay-2s;'
})
});
mainHeader.appendChild(maleButton)
mainHeader.appendChild(femaleButton)
mainHeader.appendChild(otherButton)
TA贡献1865条经验 获得超7个赞
您需要使用“this”,如果您的按钮在“div”中,请尝试编写:
this.parentElement.className = 'animated infinite bounce delay-2s;
代替:
matchedDiv.className = 'animated infinite bounce delay-2s;'
添加回答
举报