2012-10-25 53 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

相关问题