简体   繁体   English

香草 Javascript insertBefore() in for 循环

[英]Vanilla Javascript insertBefore() in for loop

I have the following html structure.我有以下 html 结构。

<div id="page1" class="page">
    <div class="firstchild"></div>
    <div class="secondchild"></div>
    <div class="thirdchild"></div>
    <div class="forthchild"></div>
</div>
<div id="page2" class="page">
    <div class="firstchild"></div>
    <div class="secondchild"></div>
    <div class="thirdchild"></div>
    <div class="forthchild"></div>
</div>
<div id="page3" class="page">
    <div class="firstchild"></div>
    <div class="secondchild"></div>
    <div class="thirdchild"></div>
    <div class="forthchild"></div>
</div>

And my javascript structure is.而我的 javascript 结构是。

var pageCLASS = frame.querySelectorAll(".page");
//var pageCLASS = frame.getElementsByClassName('page')[0];
var leng = pageCLASS.length;
for (var i = 0; i < leng; ++i) {
    var pageID = frame.getElementById('page' + (i + 1));
    var firstchild = frame.getElementsByClassName('firstchild')[0];
    var secondchild = frame.getElementsByClassName('secondchild')[0];   
    var thirdchild = frame.getElementsByClassName('thirdchild')[0];
    pageID.insertBefore(thirdchild, firstchild.nextSibling);
    //pageCLASS.insertBefore(thirdchild, firstchild.nextSibling);
}

Now I have problems with the thirdchild being moved to below the firstchild and above the secondchild in all of page1, page2, and page3 together.现在,我遇到了将第三个孩子移到第一个孩子下方和第二个孩子上方的所有 page1、page2 和 page3 的问题。 The code above only moves it in page1, but for the other 2 which does nothing.上面的代码只在 page1 中移动它,但对于其他 2,它什么也不做。 The frame shown in the source is an iframe stored on the same domain with access to it's elements.源代码中显示的框架是一个 iframe 存储在同一域中,可以访问它的元素。 Can I please get some advice on what I am doing wrong as I want to move all thirdchilds in each div to below the first child in each of their parent div?当我想将每个 div 中的所有第三个孩子移动到每个父 div 中的第一个孩子下方时,我可以就我做错了什么获得一些建议吗?

The problem you are having is that you are constantly targeting the same elements with eg您遇到的问题是您不断针对相同的元素,例如

var firstchild = frame.getElementsByClassName('firstchild')[0];

because this instruction always returns the first occurrence of such an element in the iframe and never the second or third.因为该指令总是返回iframe中此类元素的第一次出现,而不是第二次或第三次。

In order to be sure that you are targeting the correct elements you can rewrite some of your code to only search for the elements that are contained within a certain parent and not inside the whole iframe .为了确保您定位到正确的元素,您可以重写一些代码以仅搜索包含在某个父元素中而不是整个iframe中的元素。

You can then use something like this instead然后你可以使用类似这样的东西

var firstChild = pageID.querySelector('.firstchild');

which will only search for an element (the first occurrence) with class firstchild that is contained within some other element (in this case the element saved in pageID ).它将仅搜索包含在某个其他元素(在本例中为保存在pageID中的元素)中包含 class firstchild的元素(第一次出现)。

Check below (I exchanged the form for document so we can test here):检查下面(我将form换成了document ,所以我们可以在这里测试):

 var pageCLASS = document.querySelectorAll(".page"); var leng = pageCLASS.length; for (var i = 1; i <= leng; i++) { var pageID = document.getElementById('page' + i); var firstChild = pageID.querySelector('.firstchild'); var thirdChild = pageID.querySelector('.thirdchild'); firstChild.parentNode.insertBefore(thirdChild, firstChild.nextSibling); }
 .page { border: 1px solid #09f; }
 <div id="page1" class="page"> <div class="firstchild">first child</div> <div class="secondchild">second child</div> <div class="thirdchild">third child</div> <div class="forthchild">fourth child</div> </div> <div id="page2" class="page"> <div class="firstchild">first child</div> <div class="secondchild">second child</div> <div class="thirdchild">third child</div> <div class="forthchild">fourth child</div> </div> <div id="page3" class="page"> <div class="firstchild">first child</div> <div class="secondchild">second child</div> <div class="thirdchild">third child</div> <div class="forthchild">fourth child</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM