使用InsertAfter移动元素,但只插入元素的父元素?

时间:2012-11-05 04:31:54

标签: jquery

我做了以下事情:

  jQuery('.businessdirectory-category .wpbdp-rating-info').insertAfter('.businessdirectory-category .title');

.wpbdp-rating-info之后移动.title。但是页面中有3个.wpbdp-rating-info,所以现在每个.businessdirectory-category都有3 .wpbdp-rating-info

我该怎么做才能让每个.businessdirectory-category只有自己的.wpbdp-rating-info appended to its own。title`?

编辑:

enter image description here

我把它更改为:

 jQuery('.wpbdp-listing-excerpt .wpbdp-rating-info').each(function () {
    var $this = jQuery(this);
    $this.insertAfter($this.closest('.wpbdp-listing-excerpt').find('.title'));
  });

但也没有用。

1 个答案:

答案 0 :(得分:1)

您可以迭代匹配的集合并单独移动每个元素:

jQuery('.businessdirectory-category .wpbdp-rating-info').each(function () {
    var $this = jQuery(this);
    $this.insertAfter($this.closest('.businessdirectory-category').find('.title'));
});

您可以将参数简化为insertAfter,但您需要发布您的标记,以便我确定。此示例假定.title元素不是.businessdirectory-category的兄弟或祖先。

这是working example