Christopher Jordan schrieb:
> Klaus Hartl wrote:
>> Christopher Jordan schrieb:
>>   
>>> Hi folks,
>>>
>>> How can I tell what class (or classes) an element has? For instance I've 
>>> got an elements like:
>>>
>>> <div class="classA classB"></div>
>>> <div class="classA classC"></div>
>>>
>>> How can I select all elements that have classA and ClassB but not classC?
>>>     
>>
>> $('div.classA, div.classB').not('.classC')
>>
>> In case you want only elements that are of classA and classB use this:
>>
>> $('div.classA.classB')
>>   
> I had tried something like this, but didn't get the syntax quite right. 
> I wasn't caring about the div part of it and just said:
> $(.classA .classB)... with a space.

The div part (type selector) was added by me just for better performance.

The selector ".classA .classB" would select all .classB that are a 
descendant of .classA. With a comma you can group selectors.

It's pretty useful when working with jQuery to know about CSS selectors, 
even if you don't do CSS:
http://www.w3.org/TR/CSS21/selector.html

And if you want to "translate" a given selector to human readable 
english (or spanish ;-)), you can use Select'O'Racle:
http://gallery.theopalgroup.com/selectoracle/

"div.classA.classB" gives you the following for example:

Selects any div element with a class attribute that contains the word 
classA or any div element with a class attribute that contains the word 
classB.

You can even learn about selectors just by playing around with this.


>>> I'd also like to know how to select all items of a certain class and 
>>> attribute. For instance:
>>>
>>> <div class="classA" Status="On"></div>
>>> <div class="classA" Status="Off"></div>
>>> <div class="classA" Status="Off"></div>
>>> <div class="classA" Status="On"></div>
>>> <div class="classA" Status="Off"></div>
>>> <div class="classA" Status="Off"></div>
>>>
>>> I want jQuery to match all the elements that are of classA and that have 
>>> the Status attribute set to "On". How would I do that?
>>>
>>> $('div.classA').filter('[EMAIL PROTECTED]"On"]')
>>>     
> Ah! I looked at contains... didn't even see filter. My eyes must have 
> just passed over it in scanning the API.

contains is for looking up if an element in question has a particular 
child, but cannot be used for attributes.


> Thanks heaps, Klaus! I really appreciate it! :o)

Your welcome!


-- Klaus

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to