最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript childNodes does not find all children of a div when appendchild has been used - Stack Overflow

programmeradmin5浏览0评论

Alright, I am hoping someone can help me out. I apologize up front that this one may be confusing. I have included an example to try to help ease the confusion as this is better seen then heard.

I have created a webpage that contains a group or set of groups. Each group has a subgroup. In a nutshell what is happening is this page will allow me to bine multiple groups containing subgroups into a new group. The page will give the chance to rename the old subgroups before they are bined into new groups in order to avoid confusion.

When a group is renamed it will check to make sure there is not already a group with that name. If there is it will copy itself out of it's own group and into that group and then delete the original. If the group does not already exist it will create that group, copy itself in and then delete the original.

Subgroups can also be renamed at which point they will move into the group with the same name if it exists, or create a new one if it doesn't.

The page has a main div. The main div contains 'new sub group' divs. Inside each of those is another div containing the 'old sub group' divs. I use a loop through the child nodes of the 'new sub group' div when renaming a group in order to find each child node. These are then copied into a new div within the main div.

The crux of the problem is this. If I loop through a DIV and copy all of the DIVs in it into a new or existing DIV all is well. When I then try to take that DIV and copy all of it's DIVs into another or new DIV it always skips one of the moved DIVs.

For simplicity I have copied the entire working code below. To recreate the issue click the spot where the image should appear next to the name ewrewrwe and rename it to something else. All is well. Now click that new group the same way and name it something else. You will see it skip one each time.

I have linked the page here: .html

The link is clean, it is my personal website I use for a local motorycle group I am part of.

Thanks for the help everyone!!! Please let me know if I can clarify on anything.

I know the code is not the best right now, it is just demo code and my intent is to get the concept working then streamline it all.

Alright, I am hoping someone can help me out. I apologize up front that this one may be confusing. I have included an example to try to help ease the confusion as this is better seen then heard.

I have created a webpage that contains a group or set of groups. Each group has a subgroup. In a nutshell what is happening is this page will allow me to bine multiple groups containing subgroups into a new group. The page will give the chance to rename the old subgroups before they are bined into new groups in order to avoid confusion.

When a group is renamed it will check to make sure there is not already a group with that name. If there is it will copy itself out of it's own group and into that group and then delete the original. If the group does not already exist it will create that group, copy itself in and then delete the original.

Subgroups can also be renamed at which point they will move into the group with the same name if it exists, or create a new one if it doesn't.

The page has a main div. The main div contains 'new sub group' divs. Inside each of those is another div containing the 'old sub group' divs. I use a loop through the child nodes of the 'new sub group' div when renaming a group in order to find each child node. These are then copied into a new div within the main div.

The crux of the problem is this. If I loop through a DIV and copy all of the DIVs in it into a new or existing DIV all is well. When I then try to take that DIV and copy all of it's DIVs into another or new DIV it always skips one of the moved DIVs.

For simplicity I have copied the entire working code below. To recreate the issue click the spot where the image should appear next to the name ewrewrwe and rename it to something else. All is well. Now click that new group the same way and name it something else. You will see it skip one each time.

I have linked the page here: http://vtbikenight./test.html

The link is clean, it is my personal website I use for a local motorycle group I am part of.

Thanks for the help everyone!!! Please let me know if I can clarify on anything.

I know the code is not the best right now, it is just demo code and my intent is to get the concept working then streamline it all.

Share Improve this question asked Apr 16, 2010 at 17:33 yesterdayzeyesterdayze 771 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You need to decrement count after you remove the node, otherwise you skip over the node that just took the place of the removed node.

        for ( var count = 0; count < obj.childNodes.length; count++ )
        {
            if(obj.childNodes[count].tagName == 'DIV'){
                 //alert(obj.childNodes[count].tagName +" - "+obj.childNodes[count].id);
                 RenameOldSubGroup(obj.childNodes[count].id,NewNewGroupName)

                 count--;  // Decrement count to account for node you removed
            }
        }

Well in the loop, you have a counter that you increment, and you're yanking nodes out of the list. That is, obj.childNodes.length will change whenever you pull something out of the container.

发布评论

评论列表(0)

  1. 暂无评论