<ul class="gallery"></ul>
和
const images = [
{
url: ,
alt: ,
},
document.body.style.backgroundColor = "#0009";
const imageGalleryItem = document.querySelector("ul >li");
imageGalleryItem.forEach(element => {'ul.style.listStyleType = none;'})
存在问题:无法读取 null 的属性(读取“forEach”)。 您好,请问您能帮忙解决一下吗?
这意味着
imageGalleryItem
为空,因为选择器表达式不正确。如果您想选择作为 ul
元素直接子代的所有 li
元素,那么您应该使用 document.querySelectorAll("li > ul")
。
以下是修复方法(假设您选择所有
ul
元素,而不仅仅是 li
的子元素):
document.body.style.backgroundColor = "#0009";
const imageGalleryItem = document.querySelectorAll("ul");
imageGalleryItem.forEach(element => {element.style.listStyleType = "none";})