Home > Web Front-end > JS Tutorial > Precautions for using javascript removeChild_javascript tips

Precautions for using javascript removeChild_javascript tips

WBOY
Release: 2016-05-16 18:54:23
Original
989 people have browsed it

Suppose: a piece of ordinary code:
where gift_list is the id of a table

Copy code The code is as follows:

var giftBody = document.getElementById("gift_list").getElementsByTagName("tbody")[0];
var giftTrs = giftBody.getElementsByTagName("tr");
for (var i= 0;i{
giftTrs[i].removeChild(giftTrs[i]);
}

Then only the first child will be deleted at this time One row, because when one is deleted, the position of the row will move forward one position.
giftTrs.length will also be reduced by one accordingly.
So the correct operation method is:
Copy the code The code is as follows:

var giftBody = document.getElementById("gift_list").getElementsByTagName("tbody")[0];
var giftTrs = giftBody.getElementsByTagName("tr");
var len = giftTrs.length; //Need to change giftTrs The length attribute of .length is stored
for (var i=0;i{
giftBody.removeChild(giftTrs[0]);
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template