2012-10-25 41 views
1

使用jQuery/Underscore,給定以下列表。我如何才能找到那些不屬於李W內data-allow-html="true"如何找到不在數據屬性中的所有元素

<ol> 
    <li><div class="msg">message</div></li> 
    <li data-allow-html="true"><div class="msg">message</div></li> 
    <li><div class="msg">message</div></li> 
    <li data-allow-html="true"><div class="msg">message</div></li> 
    <li><div class="msg">message</div></li> 
    <li data-allow-html="true"><div class="msg">message</div></li> 
    <li><div class="msg">message</div></li> 
    <li data-allow-html="true"><div class="msg">message</div></li> 
</ol> 

所有.msg元素我目前有:

$('#mydiv').find('.msg').each(function() {} 

,並嘗試:

$('#mydiv').find('li').not('data-allow-html="true"').find('.msg').each(function() {} 

似乎這不工作。想法?由於

+0

http://stackoverflow.com/questions/2891452/jquery-data-selector –

回答

2

您可以使用以下方法來獲取所有<li> withing #mydiv,則包含屬性檢查not(),然後找到每個.msg

$('#mydiv li').not('[data-allow-html="true"]').find('.msg').each(function() { 
    // your code 
});​ 
+0

的數據不是在與.msg相同的元素,但在parent() –

1

在你代碼,這是not('data-allow-html="true"')
應該是not('[data-allow-html="true"]') .. //丟失[]

您也可以嘗試我噸這樣

$('li').not('[data-allow-html]').find('.msg').each(function() { 

     $(this).css('color', 'orange') 
     // Your code here 
}); 

// OR

$('li:not([data-allow-html])').find('.msg').each(function() { 

}); 

Check FIDDLE

相關問題