如何在JS中的特定标签上靶向ID? 我想在下面的代码中针对我的SDF-Carousel-Item上的ID。 const carouselmodal = document.queryselector(“ sdf-carousel-item”); const carouselslideone = carouselmodal.queryselector(“#

问题描述 投票:0回答:1

const carouselModal = document.querySelector("sdf-carousel-item"); const carouselSlideOne = carouselModal.querySelector("#first"); if (carouselSlideOne){ console.log("carousel slide one is here"); } else {console.log("carousel slide one not here.")}

<sdf-carousel-item id="first" style="height: auto; display: block; width: 50%;" role="group" aria-roledescription="slide" aria-label="1 of 2" class="first-in-page last-in-page active">test one</sdf-carousel-item>


我尝试的是我尝试的东西:
const carouselModal = document.querySelector("sdf-carousel-item");
const carouselSlideOne = carouselModal.querySelector("#first");
if (carouselSlideOne){
  console.log("carousel slide one is here");
} else {console.log("carousel slide one not here.")}
我认为定义CarouselModal是有意义的,然后用CarouselModal.querySelector靶向该标签上的ID是有意义的。有什么想法为什么这不起作用?我是JS的新手,所以也许我错过了一些明显的东西?之所以在这里不工作的原因是因为我收到了“旋转木马幻灯片不在这里”的控制台消息,而当它应该是“旋转木马滑动一个”时。

你必须替换

const carouselSlideOne = carouselModal.querySelector("#first");

const carouselSlideOne = document.querySelector("#first");
javascript if-statement constants jquery-selectors
1个回答
0
投票
,如预期的那样,您的控制台输出为

"carousel slide one is here"

。问题已解决。
其他选项可能是

const carouselSlideOne = document.getElementById("first");

请参见
document的文档。


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.