gpt4 book ai didi

javascript - html javascript在div中获取长度名称

转载 作者:行者123 更新时间:2023-11-30 11:18:46 25 4
gpt4 key购买 nike

我需要元素的返回计数,例如名称“A”

<div id="box">
<input name="A" value="123">
<input name="A" value="456">
</div>

<script type="text/javascript">
var ii = document.getElementById("box");
var hh = ii.getElementsByName('A');
alert(hh.length); // should return 2 but return TypeError
</script>

结果: TypeError:ii.getElementsByName 不是函数

感谢您的帮助!!

最佳答案

您需要使用 querySelectorAll('[name=A]'),因为 getElementsByName() 不是函数。看到了,它准确地给出了 Adiv 的计数,id 为 box 因为我们使用 ii.querySelectorAllii 的引用中选择元素。

var ii = document.getElementById("box");
var hh = ii.querySelectorAll('[name=A]')
alert(hh.length);
<div id="box">
<input name="A" value="123">
<input name="A" value="456">
</div>
<div id="box2">
<input name="A" value="123">
<input name="A" value="456">
</div>

关于javascript - html javascript在div中获取长度名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50600399/

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