Considering the following scenario where you have two Div tag with above classes
<div class="g"></div>
<div class="g g"></div>
Write following to get only those div which have class"g"
alert($("div[class='g']").length);
سؤال
I have a rather "weird" scenario (web scraping). I want to select class=g
only but not those with class="g g"
(double class g
). How to do that in jQuery?
If I use $('.g'), it will select both .g
and .g .g
UPDATE 1:
If you don't think .g .g
is valid, do View Source in Google search results ;)
المحلول 2
Considering the following scenario where you have two Div tag with above classes
<div class="g"></div>
<div class="g g"></div>
Write following to get only those div which have class"g"
alert($("div[class='g']").length);
نصائح أخرى
Even if I don't think g g
is valid (Let me check it)..and therefore $(".g:not(.g.g)")
wouldn't work, you could do something like:
var myWeirdElements = $('.g').map(function(){return this.className.indexOf('g g') && this ;});
// or, as mentioned on the comment, using Array.filter method
demo => http://jsfiddle.net/GeAaA/
Edit: about the 2nd comment, you just have to count how many g (simple regex)