gpt4 book ai didi

javascript - 怎么找没有。名称为 "myDIV"的类?

转载 作者:行者123 更新时间:2023-12-03 02:35:15 25 4
gpt4 key购买 nike

/*请查看这段代码并告诉我如何找到 no。类“myDIV”我也尝试过 getelementbyid 和 childNodes.length 但没有任何效果*/

<div id="example">First div element with class="example"
<div class="fexample">First div element with class="example"
<div class="myDIV">
<p>First p element in div.</p>
<p>Another p element in div.</p>
</div>
<div class="myDIV">
<p>First p element in div.</p>
<p>Another p element in div.</p>
</div>
<div class="myDIV">
<p>First p element in div.</p>
<p>Another p element in div.</p>
</div>
</div>
</div>
<script>
function myFunction() {
var a = document.getElementsByClassName("myDIV");
document.getElementById("demo").innerHTML = a.length;
}
</script>
</body>

最佳答案

使用document.querySelectorAll()要构建包含该类的所有元素的列表,请使用 .length 属性返回金额。

var elements = document.querySelectorAll('.myDIV');
console.log(elements.length)
<div id="example">First div element with class="example"
<div class="fexample">First div element with class="example"
<div class="myDIV">
<p>First p element in div.</p>
<p>Another p element in div.</p>
</div>
<div class="myDIV">
<p>First p element in div.</p>
<p>Another p element in div.</p>
</div>
<div class="myDIV">
<p>First p element in div.</p>
<p>Another p element in div.</p>
</div>
</div>
</div>

关于javascript - 怎么找没有。名称为 "myDIV"的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48561226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com