Javascript 阅读a的HTML子项<;UL>;收集

Javascript 阅读a的HTML子项<;UL>;收集,javascript,html,asp.net,Javascript,Html,Asp.net,我试图在运行时通过循环检索父节点的所有子节点。但是,循环将永远运行,导致浏览器立即崩溃。我做错了什么 HTML: 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.f

我试图在运行时通过循环检索父节点的所有子节点。但是,循环将永远运行,导致浏览器立即崩溃。我做错了什么

HTML:

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
}
//试图检索每个
  • 子项的输入字段中的每个值 var idbox=document.getElementById(“新ul”); while(idbox.firstChild){ console.log(idbox.firstChild); //浏览器在此崩溃 }
  • 您可以像这样轻松实现它

    Array.prototype.map.call( document.querySelectorAll("#new-ul input"), function( element,index ){
    
        console.log( element.value );
    
    });
    

    小提琴:

    你可以很容易地做到这一点

    Array.prototype.map.call( document.querySelectorAll("#new-ul input"), function( element,index ){
    
        console.log( element.value );
    
    });
    

    小提琴:

    你可以很容易地做到这一点

    Array.prototype.map.call( document.querySelectorAll("#new-ul input"), function( element,index ){
    
        console.log( element.value );
    
    });
    

    小提琴:

    你可以很容易地做到这一点

    Array.prototype.map.call( document.querySelectorAll("#new-ul input"), function( element,index ){
    
        console.log( element.value );
    
    });
    

    Fiddle:

    您所做的错误是创建了一个无限循环,因为while循环的条件将始终计算为true。相反,你想这么做吗

    var parent = document.getElementById('new-ul');
    var children = parent.children;
    for (var i = 0; i < children.length; i++) {
      console.log(children[i]);
      // Do stuff
    }
    
    var parent=document.getElementById('new-ul');
    var children=parent.children;
    对于(变量i=0;i

    您所做的错误是创建了一个无限循环,因为while循环的条件将始终计算为true。相反,你想这么做吗

    var parent = document.getElementById('new-ul');
    var children = parent.children;
    for (var i = 0; i < children.length; i++) {
      console.log(children[i]);
      // Do stuff
    }
    
    var parent=document.getElementById('new-ul');
    var children=parent.children;
    对于(变量i=0;i

    您所做的错误是创建了一个无限循环,因为while循环的条件将始终计算为true。相反,你想这么做吗

    var parent = document.getElementById('new-ul');
    var children = parent.children;
    for (var i = 0; i < children.length; i++) {
      console.log(children[i]);
      // Do stuff
    }
    
    var parent=document.getElementById('new-ul');
    var children=parent.children;
    对于(变量i=0;i

    您所做的错误是创建了一个无限循环,因为while循环的条件将始终计算为true。相反,你想这么做吗

    var parent = document.getElementById('new-ul');
    var children = parent.children;
    for (var i = 0; i < children.length; i++) {
      console.log(children[i]);
      // Do stuff
    }
    
    var parent=document.getElementById('new-ul');
    var children=parent.children;
    对于(变量i=0;i
    在jquery中使用:

    $(document).ready(function() {
        $("#new-ul input").each(function(){
            console.log($(this).val())
        });
    });
    
    在jquery中使用:

    $(document).ready(function() {
        $("#new-ul input").each(function(){
            console.log($(this).val())
        });
    });
    
    在jquery中使用:

    $(document).ready(function() {
        $("#new-ul input").each(function(){
            console.log($(this).val())
        });
    });
    
    在jquery中使用:

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

    可以通过jquery$轻松完成

    $("#your-ul-id li").each(function(){ 
        console.log($(this)) 
    });
    

    可以通过jquery$轻松完成

    $("#your-ul-id li").each(function(){ 
        console.log($(this)) 
    });
    

    可以通过jquery$轻松完成

    $("#your-ul-id li").each(function(){ 
        console.log($(this)) 
    });
    

    可以通过jquery$轻松完成

    $("#your-ul-id li").each(function(){ 
        console.log($(this)) 
    });
    

    请参阅:Well
    idbox.firstChild
    始终有一个
    firstChild
    ,因此您正在创建一个无止境的循环请参阅:Well
    idbox.firstChild
    始终有一个
    firstChild
    ,因此您正在创建一个无止境的循环请参阅:Well
    idbox.firstChild
    始终有一个
    firstChild
    ,因此,您正在创建一个无止境的循环。请参阅:嗯
    idbox.firstChild
    始终有一个
    firstChild
    ,因此您正在创建一个无止境的循环