奇怪的jQuery .insertAfter行为(完全混淆DOM结构)

时间:2011-11-01 18:05:27

标签: jquery html

不知道为什么,但是

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

}

基本上是做它应该做的事情,但是它也改变了许多其他事情的顺序。就像真的很奇怪一样。

很难通过粘贴的代码演示这一点,我认为只要在视觉上你就能看到奇怪的东西:

之前 enter image description here enter image description here

正如你所看到的,它几乎随机地将元素从div“postInfo forVideo”中拉出来。 你可以在这里看到它(现在,正如这个网站正在开发中):http://syndex.me

例如,为什么它会拉出“pagecurl”div中的多边形元素,然后在我使用的span之后抛出它?太奇了!

1 个答案:

答案 0 :(得分:1)

nth-child将选择作为其各自父母的第二个孩子的所有元素。你可能只想要元素的第二个孩子。

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