remove all
  • from
      ?
  • 后端 未结 7 996
    北荒
    北荒 2020-12-15 02:48

    I am appending li in a ul using the following code:

    for (var i = 1; i <= len; i++) {
        li = document.createElement(\'li\');
    
              
    
    
            
    相关标签:
    7条回答
    • 2020-12-15 03:24
      $('#button').on('click', function() {
          $(root).empty();
      });
      
      0 讨论(0)
    • 2020-12-15 03:33

      What about?

          var ul = root;
          ul.innerHTML = '';
      
      0 讨论(0)
    • 2020-12-15 03:33

      You need to fetch the elements before removing them as the native DOM methods (most of them anyway) can't be passed in selector strings the same way jQuery's methods can.

      var lis = root.getElementsByTagName("li");
      
      for(var i = 0, il = lis.length;i<il;i++) {
          root.removeChild(lis[i]);
      }
      
      0 讨论(0)
    • 2020-12-15 03:35

      You appear to be trying this with raw JavaScript:

      while( root.firstChild ){
        root.removeChild( root.firstChild );
      }
      

      jQuery will only slow you down here.

      0 讨论(0)
    • 2020-12-15 03:37
      document.getElementById("the_ul_ID").innerHTML = "";
      
      0 讨论(0)
    • 2020-12-15 03:42

      If you are using jQuery, why don't you use it's benefits?

      adding <li> elements:

      $("<li><img src='"+path[i]+"'></li>").appendTo(root);
      

      removing all <li> elements:

      $(root).empty();
      

      deleting one <li> element:

      $("li:eq(3)",$(root)).remove();
      

      and if you are using raw js, you can use:

      document.getElementById("root").innerHTML = "";
      
      0 讨论(0)
    提交回复
    热议问题