无法删除所有childNodes

时间:2014-10-12 16:32:33

标签: javascript

我想从其parentNode中删除所有带有class="child"的子节点,但只删除了奇数序列。如何在原生javascript中删除class="child"的所有元素以及为什么只删除奇数序列?

var child = document.getElementsByClassName("child");

function remove(){
  for (var i=0; i<child.length; i++) {
    child[i].parentNode.removeChild(child[i]);
  }
}
document.getElementById("del").onclick = remove;
<div>
  <div class="child">
    Child 1
  </div>
  <div class="child">
    Child 2
  </div>
  <div class="child">
    Child 3
  </div>
  <div class="child">
    Child 4
  </div>
</div>

<br/>

<div>
  <div class="child">
    Child 5
  </div>
  <div class="child">
    Child 6
  </div>
</div>

<button id="del">Remove Child</button>

4 个答案:

答案 0 :(得分:4)

getElementsByClassName()返回一个NodeList,它是一个类似于数组的对象,可以显示结果的实时视图。

从这些元素中删除类时,元素将从NodeList中删除。

child[0]移除课程后,child缩小,child[0]成为下一个元素。

有很多方法可以解决这个问题:

  • 将NodeList复制到一个数组,以便它不会在你下面发生变化(例如,child = Array.prototype.slice.call(child)

  • 向后循环,这样您就不会受到变化指数的影响

  • 始终从child[0]删除该类,直到NodeList为空。

答案 1 :(得分:2)

首选使用querySelector

var children = document.querySelectorAll('div .child');
for (var i = 0; i < children.length; i++)
    children[i].parentNode.removeChild(children[i]);

答案 2 :(得分:0)

简单干净......

&#13;
&#13;
var child = document.getElementsByClassName("child");

function remove(){
  var l = child.length;
  for (var i=0; i<l; i++) {
    child[0].parentNode.removeChild(child[0]);
  }
}
document.getElementById("del").onclick = remove;
&#13;
  <div class="child">
    Child 1
  </div>
  <div class="child">
    Child 2
  </div>
  <div class="child">
    Child 3
  </div>
  <div class="child">
    Child 4
  </div>
</div>

<br/>

<div>
  <div class="child">
    Child 5
  </div>
  <div class="child">
    Child 6
  </div>
</div>

<button id="del">Remove Child</button>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

其中一个动态更改了DOM树,而另一个则没有。

     command: 'c:\users\mattz\pycharmprojects\funny\venv\scripts\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\mattz\\App
Data\\Local\\Temp\\pip-install-l36sff3k\\pygame\\setup.py'"'"'; __file__='"'"'C:\\Users\\mattz\\AppData\\Local\\Temp\\pip-install-l36sff3k\\pygame\\setup.p
y'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'
exec'"'"'))' egg_info --egg-base 'C:\Users\mattz\AppData\Local\Temp\pip-pip-egg-info-ig5yoiix'
         cwd: C:\Users\mattz\AppData\Local\Temp\pip-install-l36sff3k\pygame\
    Complete output (17 lines):
    
    
    WARNING, No "Setup" File Exists, Running "buildconfig/config.py"
    Using WINDOWS configuration...
    
    
    Download prebuilts to "prebuilt_downloads" and copy to "./prebuilt-x64"? [Y/n]Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\mattz\AppData\Local\Temp\pip-install-l36sff3k\pygame\setup.py", line 194, in <module>
        buildconfig.config.main(AUTO_CONFIG)
      File "C:\Users\mattz\AppData\Local\Temp\pip-install-l36sff3k\pygame\buildconfig\config.py", line 210, in main
        deps = CFG.main(**kwds)
      File "C:\Users\mattz\AppData\Local\Temp\pip-install-l36sff3k\pygame\buildconfig\config_win.py", line 576, in main
        and download_win_prebuilt.ask(**download_kwargs):
      File "C:\Users\mattz\AppData\Local\Temp\pip-install-l36sff3k\pygame\buildconfig\download_win_prebuilt.py", line 302, in ask
        reply = raw_input(
    EOFError: EOF when reading a line
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
int a, b, a2, b2;
cin >> a;
cin >> b;
a2 = a;
b2 = b;

while(a!=b) {
    if(a>b) b+=b2;
    if(b>a) a+=a2;
}
cout << a << " X " << b << endl;
cred = Google::Cloud::Firestore::Credentials.new "path/to/myJsonSerivcesKey.Json",  scope: "https://www.googleapis.com/auth/datastore"