元素 ul 的样式[重复]

问题描述 投票:0回答:1
<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”)。 您好,请问您能帮忙解决一下吗?

javascript html-lists
1个回答
0
投票

这意味着

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";})
© www.soinside.com 2019 - 2024. All rights reserved.