我正在尝试创建一个类似电子商务的网站,在那里我需要在列表的标题和描述之间建立关系。问题是,我不知道如何可靠地做到这一点,因为有些房源有描述,有些则没有。因此,我无法可靠地使用for循环来将标题与描述关联起来。你能提出你的建议吗?
这就是我的想法,但它并不可靠:
const allTitles= document.querySelectorAll(".title");
const arrayOfTitlesAndDescription= allTitles.map((title,index) => { title :document.querySelectAll(".description")[index] } )
非常感谢!
如果问题不清楚,这张图片将帮助您更好地理解
编辑:
const allCards= document.querySelectorAll(".card");
/// also tried with allCards directly
Array.from(allCards).forEach(card => card.querySelector(".description"));
/// This works, but I don't know how to tell which .card is which
document.querySelectorAll(".card").querySelector(".description")
搜索包含.title
和.description
的某个div标记,并搜索每个此类标记的子标记是否有任何描述。