Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery-before()和after()的不同行为_Javascript_Jquery - Fatal编程技术网

Javascript jQuery-before()和after()的不同行为

Javascript jQuery-before()和after()的不同行为,javascript,jquery,Javascript,Jquery,我有一个可排序的列表(不是jQueryUISortable,只需单击按钮)。 通过after()向右排序效果很好,但使用before()向左排序不起作用 首先,代码: Html 照片列表: <ul class="PhotoList"> <li> <div class="ThumbWrapper"> <img src="images/img_testphoto.jpg" /> </div&

我有一个可排序的列表(不是jQueryUISortable,只需单击按钮)。 通过
after()
向右排序效果很好,但使用
before()
向左排序不起作用

首先,代码:

Html

照片列表:

<ul class="PhotoList">
   <li>
       <div class="ThumbWrapper">
           <img src="images/img_testphoto.jpg" />
       </div>
   </li>
   [...more Elements...]
</ul>
功能

function sortPolaroid(elm, right)
{
    var selectitem = elm.parents('li');
    var index = jQuery('.SelectablePolaroids').children().index(selectitem);
    var photo = jQuery('.PhotoList').children().eq(index);
    selectitem.remove();
    photo.remove();
    if(right)
    {
        jQuery('.SelectablePolaroids').children().eq(index).after(selectitem);
        jQuery('.PhotoList').children().eq(index).after(photo);
    }
    else
    {
        console.log(index);
        jQuery('.SelectablePolaroids').children().eq(index).before(selectitem);
        jQuery('.PhotoList').children().eq(index).before(photo);
    }
 }
如果
right
为true或单击了
.ArrowRight
,则其工作原理与预期相同。项目将被移除并插入到更右侧的一个位置。 如您所见,我记录了
index
,以检查else语句是否执行以及索引是否正确。是的,else语句被执行,索引也正确。是
before()
以另一种方式工作而不是
before()
还是我对另一个错误视而不见

编辑:

<ul class="PhotoList">
   <li>
       <div class="ThumbWrapper">
           <img src="images/img_testphoto.jpg" />
       </div>
   </li>
   [...more Elements...]
</ul>
我还记录了
before()之后的索引

它保持不变,没有任何变化。。。但它是删除的,这意味着它被插入到完全相同的位置,因此如果我执行以下操作-我的解决方案,它就会工作:

//...
else {
    jQuery('.SelectablePolaroids').children().eq(index-1).before(selectitem);
    jQuery('.PhotoList').children().eq(index-1).before(photo);
}
但我真的不知道为什么。。。新元素应该在索引处,而before应该在索引之前插入它。。。 如果有人知道,请回答我,我会很高兴地接受一个回答:-)


好吧,我自己想出来了,回答说,很抱歉吵闹,但也许其他人也不知道,他们的想法和我一样:-)

photo.remove()
实际上从树中删除了dom元素,因此我认为索引不匹配。尝试使用before/after而不删除,这样只会移动元素。

通过删除元素,下一个而不是上一个元素将获得索引,因此它将插入到相同的位置。 它与
after()
一起工作,因为获取新索引的元素位于“正确的方向”,因此它与以下元素一起工作:

//...
else
{
    jQuery('.SelectablePolaroids').children().eq(index-1).before(selectitem);
    jQuery('.PhotoList').children().eq(index-1).before(photo);
}

只是一个逻辑错误。。。经过适当思考后解决:-)

这不是问题,在删除下一个元素后,该元素具有selectitem的前一个索引,因此我用after()和before()插入它-数字存储在索引中。正如我所描述的,after()也可以正常工作。。。。但我试过了,事实上这些元素在执行时被删除了,可能是因为没有创建新的DOM元素,也不能在它之后插入。。。必须将其删除并插入到该索引处的新元素之后。。。。它至少对after()有效-但我真的很感谢你的帮助:-)我已经编辑了我的问题,现在有效了,很抱歉吵闹-但我还不确定它为什么有效,所以你可能有一个线索?;-)
//...
else {
    jQuery('.SelectablePolaroids').children().eq(index-1).before(selectitem);
    jQuery('.PhotoList').children().eq(index-1).before(photo);
}
//...
else
{
    jQuery('.SelectablePolaroids').children().eq(index-1).before(selectitem);
    jQuery('.PhotoList').children().eq(index-1).before(photo);
}