我正在创建一个电子商务网站。在这个电子商务网站,我有一个产品页面。这个产品页面包含多种类型的产品。
我创建了一些独立的按钮,用户可以点击这些按钮来显示不同的产品。例如,当用户点击标题为"Xbox"的按钮时,只有平台为"Xbox"的产品才会显示出来。标题为"Xbox"的按钮的颜色也会变成红色,其他按钮的颜色也会变成白色。
我写了下面的代码:
<div id="buttons">
<button class="button-value" onclick="filterProduct('all')">All</button>
<button class="button-value" onclick="filterProduct('xbox')">Xbox</button>
<button class="button-value" onclick="filterProduct('playstation-5')">PlayStation 5</button>
</div>
<div id="products"></div>
#buttons {
text-align: center;
}
.button-value {
border: 2px solid #ff0000;
padding: 1em 2.2em;
border-radius: 3em;
background-color: transparent;
color: #ff0000;
cursor: pointer;
}
.active {
background-color: #ff3333;
color: #ffffff;
}
let products = {
data: [
{
productName: "Alan Wake Remasterd",
developer: "Epic Games",
genre: "",
pegi: "16",
platforms: ["Xbox Series X"],
price: "24.99",
image: "images/images/xbox/alan-wake-remastered-xbox.jpg",
},
{
productName: "Battlefield 2042",
developer: "Electronic Arts",
genre: "",
pegi: "16",
platforms: ["Xbox Series X", "PlayStation 5"],
price: "24.99",
image: "images/images/xbox/battlefield-2042-xbox.jpg",
},
],
};
for (let i of products.data) {
// Create Card
let card = document.createElement("div");
// Card should have a category and should stay hidden initialy
card.classList.add("card", i.category, "hide");
// image div
let imgContainer = document.createElement("div");
imgContainer.classList.add("image-container");
// img tag
let image = document.createElement("img");
image.setAttribute("src", i.image);
imgContainer.appendChild(image);
card.appendChild(imgContainer);
// Container
let container = document.createElement("div");
container.classList.add("container");
// Product Name
let name = document.createElement("h5");
name.classList.add("product-name");
name.innerText = i.productName.toUpperCase();
container.appendChild(name);
// Product Price
let price = document.createElement("h6");
price.innerText = "$" + i.price;
container.appendChild(price);
card.appendChild(container);
document.getElementById("products").appendChild(card);
}
// Parameter passed from button (Parameter same as category)
function filterProduct(value) {
// Button Class Code
let buttons = document.querySelectorAll(".button-value");
buttons.forEach((button) => {
// Check if value equals innerText
if (value.toUpperCase() == button.innerText.toUpperCase()) {
button.classList.add("active");
} else {
button.classList.remove("active");
}
});
// Select all elements
let elements = document.querySelectorAll(".card");
// Loop through the elements
elements.forEach((element) => {
// Display all cards on all button click
if (value == "all") {
element.classList.remove("hide");
} else {
// Check if element contains category class
if (element.classList.contains(value)) {
// Display elements based on category
element.classList.remove("hide");
} else {
// Hide other elements
element.classList.add("hide");
}
}
});
}
function filterProduct(value) {
// Button Class Code
let buttons = document.querySelectorAll(".button-value");
buttons.forEach((button) => {
// Check if value equals innerText
if (value.toUpperCase() == button.innerText.toUpperCase()) {
button.classList.add("active");
} else {
button.classList.remove("active");
}
});
此代码生成以下输出:Output of my HTML, CSS and JavaScript code
但是,当我点击标题为"Xbox"的按钮时,按钮的颜色会改变,但没有显示任何卡片。当我点击标题为"PlayStation 5"的按钮时,按钮的颜色不会改变,也没有显示卡片。
我希望颜色的"playstation 5"按钮也显示,并为所有的卡需要显示,显示。
1条答案
按热度按时间jm2pwxwz1#
这是粘贴到控制台,如果你想尝试一下。