jQuery: filter out elements via class?

后端 未结 3 615
无人共我
无人共我 2020-12-29 06:53

I have a website featuring a long list of names. To make it more oversee-able, I\'d like to put a text link in to

(on load) show all

(on clicking word \"pea

3条回答
  •  借酒劲吻你
    2020-12-29 07:04

    To filter out elements that contain a given class or any other attribute value, using the Attribute Contains Word Selector would be a good solution:

    $("span").filter("[class~='apple']")
    

    Actually, for the class attribute, it's even easier to just write:

    $("span").filter(".apple") // or:
    $("span.apple")
    

    [This is also what Joel Potter wrote in his comment to this answer.]

    That way you'll be able to match all of the below:

    ...
    ...
    ...
    

    So whenever you're not 100% sure that you'll only have a single class set on an element, use the ~= operator.

提交回复
热议问题