为什么jQuery在.insertAfter期间重复div?

时间:2015-10-20 05:26:33

标签: javascript jquery html css

我有3个li,执行.insertAfter()

后重复内容
<ul class="course-list">
    <li>

     <div class="course-image">
      <a href="#"><img src="url_of_photo" /></a>
     </div>

     <div class="course-content">
      <p class="post-title"><a title="" href="/courses/level-2-certificate-in-english/"> Level 2 Certificate In English</a>
      </p>

      <p class="post-meta post-meta-level">Level 2</p>

      <p class="post-meta post-meta-boxcolor">light blue</p>
     </div>

    </li>
</ul>

我想在class =“course-content”

下面转移class =“course-image”

执行时:

 $('.course-list .course-image').insertAfter('.course-list .course-content');

结果变为如下:

<ul class="course-list">
    <li>

     <div class="course-content">
      <p class="post-title"><a title="Level 2 Certificate In English For Business" href="/courses/level-2-certificate-in-english/"> Level 2 Certificate In English</a>
      </p>

      <p class="post-meta post-meta-level">Level 2</p>

      <p class="post-meta post-meta-boxcolor">light blue</p>
     </div>

     <div class="course-image">
      <a href="#"><img src="url_of_photo" /></a>
     </div>

     <div class="course-image">
      <a href="#"><img src="url_of_photo" /></a>
     </div>

    </li>
</ul>

它发生在那个ul的每个人身上。

为每个列表移动class =“course-image”的正确查询是什么,而不必重复evey li?

谢谢!

1 个答案:

答案 0 :(得分:3)

你需要迭代并移动

$('.course-list .course-image').each(function(){
    $(this).insertAfter($(this).next())
});