jQuery: filter out elements via class?

后端 未结 3 619
无人共我
无人共我 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:21

    Just bumped into this post, I know it's old but to be honest are none of the given answers pretty helpful. In my opinion, you can filter out the elements using the filter with :not, as in filter(':not()').

    As Joel Potter stated, using $("span[class='apple']").hide(); will only select the spans with exactly one classname, apple. If multiple classes are present (which is highly likely) then such an approach would not work.

    If you click on a word, e.g. pears, you can then filter out the elements that do not contain the class pears.

    $('span').show().filter(':not(.pears)').hide();
    

    and you're done ;)

提交回复
热议问题