gpt4 book ai didi

javascript - 读取
    集合的 HTML 子级

转载 作者:行者123 更新时间:2023-11-28 18:58:33 24 4
gpt4 key购买 nike

我正在尝试检索所有 <li>父节点的子节点 <ul>节点在运行时通过循环。然而,循环永远运行,导致浏览器立即崩溃。我做错了什么?

HTML:

<ul id="new-ul">
<li> <input value="323" /> </li>
<li> <input value="421" /> </li>
<li> <input value="547" /> </li>
</ul>

Javascript:

//Attempting to retrieve each value in input fields of each <li> child
var idbox = document.getElementById("new-ul");
while (idbox.firstChild) {
console.log(idbox.firstChild);
//Browser crashes here
}

最佳答案

在jquery中使用:

$(document).ready(function() {
$("#new-ul input").each(function(){
console.log($(this).val())
});
});

关于javascript - 读取 <UL> 集合的 HTML 子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33076679/

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