2 回答

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
這是一個(gè)奇怪的說法
btnContainer[i].innerHTML = Quotes[x]
如果它起作用了,你的按鈕就會(huì)消失
要在下面使用我的代碼,您需要
用名為 data-select 的數(shù)據(jù)屬性替換 onclicks 并添加
active
到“all”按鈕更改示例 div 以匹配他們的類
display: none; /* Hidden by default */
從 filterDiv 類中刪除將 .show 更改為 .hide
像這樣
.hide {
display: none;
}
完整代碼
const filterSelection = sel => { // passing a string
[...document.querySelectorAll(".filterDiv")].forEach(div => {
div.classList.toggle("hide",
sel !== "all" && !div.classList.contains(sel) // hide if not "all" and no match
)
})
};
window.addEventListener("load", function() { // when the page loads
const btnContainer = document.getElementById("myBtnContainer");
// btnContainer.innerHTML += Quotes[0]; // was x
btnContainer.addEventListener("click", function(e) { // any click on the button container
const tgt = e.target;
if (tgt.classList.contains("btn")) { // check if tgt is a button
filterSelection(tgt.getAttribute("data-select")); // grab the attribute
const current = document.querySelector(".btn.active"); // get the active
current.classList.remove("active"); // remove the active
tgt.classList.add("active"); // add active to button clicked
}
})
})
filterSelection("all");
body {
background-color: #383838;
}
.Navbar {
background-color: rgb(26, 25, 25);
overflow: hidden;
margin: -16px -8px;
}
.Navbar a {
float: left;
color: #f2f2f2;
text-align: center;
text-decoration: none;
padding: 18px 20px;
font-size: 15px;
font-family: Arial;
font-weight: bold;
line-height: 20px;
}
.Navbar a:hover {
background-color: #333;
color: #f2f2f2;
}
.container {
overflow: hidden;
font-family: Arial;
font-size: 15px;
font-weight: bold;
}
.filterDiv {
float: left;
background-color: rgb(26, 25, 25);
color: #ffffff;
width: 100px;
line-height: 100px;
text-align: center;
margin: 4px;
/* display: none; */ /* Hidden by default */
}
.hide {
display: none;
}
/* Style the buttons */
.btn {
border: none;
outline: none;
padding: 12px 16px;
background-color: rgb(31, 31, 31);
cursor: pointer;
color: white;
font-family: Arial;
}
/* Add a light grey background on mouse-over */
.btn:hover {
background-color: rgb(46, 46, 46);
}
/* Add a dark background to the active button */
.btn.active {
background-color: #666;
color: rgb(31, 31, 31);
}
.navbar2 {
background-color: rgb(31, 31, 31);
overflow: hidden;
height: 40px;
margin: 16px -8px;
}
a {
text-decoration: none;
}
h1 {
font-family: Arial;
text-align: center;
color: white;
}
.uiltje {
margin: 20px -8px;
}
<div id="myBtnContainer">
<button class="btn active" data-select="all"> Show all</button>
<button class="btn" data-select="games"> Games</button>
<button class="btn" data-select="ai"> AI</button>
<button class="btn" data-select="tools"> Tools</button>
</div>
<div class="container">
<div class="filterDiv tools">Sample Tool</div>
<div class="filterDiv games">Sample Game</div>
<div class="filterDiv ai">Sample AI</div>
</div>
添加回答
舉報(bào)