Select elements that don't have specific words in their class attribute
How do I select only <a>
elements which doesn't have the word gallery inside its id开发者_运维百科 or class attribute?
Try this selector:
a:not([id*=gallery], [class*=gallery])
This will select each a
element that does not have “gallery” in either its id
or its class
atttribute value.
Or for a full ID or class name match:
a:not([id=gallery], [class~=gallery])
This will select each a
element that does not have “gallery” as its ID or as a class name.
One way is to go about like this:
$('a').each(function(){
if ($(this).attr('class').indexOf('gallery') == -1 && $(this).attr('id').indexOf('gallery') == -1) {
// your code....
}
});
Use the not() method http://api.jquery.com/not/
$("a").not(document.getElementById('gallery'))
there is a hasClass selector in jquery you can use that. try this.
check for class only
$('a').each(function(){
if (!$(this).hasClass('gallery'))
{
//code here
}
});
or check for both class and id
$('a').each(function(){
if (!$(this).hasClass('gallery') && $(this).attr('id').indexOf('gallery') == -1)
{
//code here
}
});
精彩评论