What is jQuery has id equivalent of the following statement?
$('#mydiv').hasClass('foo')
so you could give a class name and check that it contains a supplied id.
比如:
$('.mydiv').hasId('foo')
What is jQuery has id equivalent of the following statement?
$('#mydiv').hasClass('foo')
so you could give a class name and check that it contains a supplied id.
比如:
$('.mydiv').hasId('foo')
You can bake that logic into the selector by combining multiple selectors. For instance, we could target all elements with a given id, that also have a particular class:
$("#foo.bar"); // Matches <div id="foo" class="bar">
这看起来应该类似于你用CSS写的东西.请注意,它不会应用于所有#foo
个元素(尽管应该只有一个),也不会应用于所有.bar
个元素(尽管可能有很多).它将只引用符合这两个属性的元素.
jQuery also has a great .is
method that lets your determine whether an element has certain qualities. You can test a jQuery collection against a string selector, an HTML Element, or another jQuery object. In this case, we'll just check it against a string selector:
$(".bar:first").is("#foo"); // TRUE if first '.bar' in document is also '#foo'