简体   繁体   中英

How to find all elements that are not within a data-attribute

Using jQuery / Underscore, Given the following list. How can I find all .msg elements that are not within an li 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>

I currently have:

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

And have tried:

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

Which does not seem to work. Ideas? Thanks

You can use the following to get all the <li> withing #mydiv , then checking for not() containing the attribute, then finding each .msg

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

In your code this is not('data-allow-html="true"')
supposed to be not('[data-allow-html="true"]') .. // Missing []

You can also try it this way

 $('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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM