【发布时间】:2022-10-12 21:27:39
【问题描述】:
我正在尝试制作一个电影网站,并添加了一些类别。但是你知道电影/连续剧没有一个类别。我需要添加多个。我试着做category: "Mind-Bending","Ominous", 它没有用。
const posters = [
{
title: "Dark",
category: "Mind-Bending",
img: "https://i.hizliresim.com/kl5ikc6.png",
desc: `A family saga with a supernatural twist, set in a German town where the disappearance of two young,
children exposes the relationships among four families.`
}]
const btnContainerDOM = document.querySelector(".categorie-cont")
const moviesContainerDOM = document.querySelector(".movies")
const btnList = ["All","Mind-Bending","Chilling","Ominous"]
btnList.forEach((value) => {
let button = document.createElement("button")
button.innerHTML = value
button.setAttribute("data-id",value)
button.addEventListener("click",buttonClick)
btnContainerDOM.append(button)
})
function buttonClick(){
const result = (this.getAttribute("data-id") === "All") ? posters : posters.filter((item)
=> {
return item.category == this.getAttribute("data-id")
});
addMovie(result)
}
【问题讨论】:
-
如果您希望您的类别包含多个类别,那么您只需将
category定义为一个可以包含 0 到多个类别的数组。 -
您也应该共享您的 HTML。这可以帮助你了解你在苦苦挣扎
-
我不明白该怎么做
-
应该使用类别作为数组并使用逻辑来检查您的对象项目子类别列表是否在主类别列表中
标签: javascript