即時通訊使用以下包裹一類的「孩子」的所有項目用一個div一類「父」的:jQuery的wrapAll孩子,如果他們不已經有某一類父
$(".child").wrapAll('<div class="parent" />');
的問題是該函數有時會重新運行,而我不希望創建多個div.parents'。我怎樣才能添加一個條件到我的代碼,所以div.parent只是添加如果div.childs'沒有父'具有'父'類?由於
即時通訊使用以下包裹一類的「孩子」的所有項目用一個div一類「父」的:jQuery的wrapAll孩子,如果他們不已經有某一類父
$(".child").wrapAll('<div class="parent" />');
的問題是該函數有時會重新運行,而我不希望創建多個div.parents'。我怎樣才能添加一個條件到我的代碼,所以div.parent只是添加如果div.childs'沒有父'具有'父'類?由於
您可以使用hasClass()
這樣的:
if (! $(".child").parent().hasClass('parent')){
$(".child").wrapAll('<div class="parent" />');
}
或者使用closest
與length
:
if (! $(".child").closest('.parent').length){
$(".child").wrapAll('<div class="parent" />');
}
不知道是不是快:
$(":not(.parent) > .child").wrapAll('<div class="parent" />');
沒有測試!但可能工作
你能做到這一點在單排)
$( 「孩子 」)父母( 'P:不(.parent)'。)找到(「 子」)wrapAll。 ( '');