简体   繁体   中英

Odd jQuery .insertAfter behaviour (messes up DOM structure completley)

No idea why, but

if ($(this).find(':nth-child(2)').is("span")) {
    var moveForward = $(this).find(".postInfo.forVideo");
$(this).find(':nth-child(2)').insertAfter(moveForward);

}

Is basically doing what it should, but it also changes the order of many other things. Like in a really really odd way.

It's hard to demo this via pasted code, i think just visually you'll be able to see the weirdness:

Before 在此处输入图片说明 After 在此处输入图片说明

As you can see, it's almost randomly pulling elements out of the div "postInfo forVideo". You can see it here (for now, as this site is in development) here: http://syndex.me .

For example, why would it pull out the polygon element which is in the "pagecurl" div and then throw that after the span i'm working with? So odd!

nth-child will select all elements that are the second child of their respective parent. You probably just want the second child of the element.

var secondChild = $(this).children().eq(2);
if (secondChild.is("span")) {
    var moveForward = $(this).find(".postInfo.forVideo");
    secondChild.insertAfter(moveForward);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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