2012-11-05 90 views
0

我做了以下內容:使用InsertAfter移動元素,但只插入元素的父元素?

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

.title後移動.wpbdp-rating-info。但是,所以現在各.businessdirectory-category已經3 01​​

我怎樣才能做到這一點,使每個.businessdirectory-category徒有其自己的.wpbdp評級,資訊appended to its own .title`有在第3頁.wpbdp-rating-info

編輯:

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

您可以通過匹配的集迭代,並單獨移動每一個元素:

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

+0

謝謝!但奇怪的是,它似乎沒有工作。也許你需要知道結構? (請參閱我的編輯)。也許是因爲WordPress不允許美元符號? – alexchenco

+0

根據你的DOM結構,'.wpbdp-listing-excerpt'不存在。這個元素有兩個類名,'wpbdp-listing'和'excerpt'。儘管你可以將'.listing-details'傳遞給'.closest()',因爲它不像DOM樹那麼遠。 –

+0

哦,它在那裏(只是截圖太小)。我得到這個:'未捕獲的SyntaxError:控制檯意外的輸入結束'雖然。 – alexchenco