jQuery:通过类过滤掉元素? [英] jQuery: filter out elements via class?

查看:98
本文介绍了jQuery:通过类过滤掉元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,其中列出了很多名称. 为了使其更易于监控,我想在

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

(加载时)显示全部

(单击豌豆"一词)隐藏所有带有class ="apple"的元素

(on clicking word "pears") hide all elements with class="apple"

(单击单词苹果"时)隐藏所有带有class ="pear"的元素

(on clicking word "apples") hide all elements with class="pear"

(单击显示全部")显示全部

(on clicking "show all") show all

我想这就像是键入时"过滤的简化版本.

I suppose it'd be like a really simplified version of "as you type" filtering.

是否存在用于此目的的插件?我什至不知道从哪里开始!

Does a plug-in exist for this? I don't even know where to start!

推荐答案

刚刚碰到这篇文章,我知道它已经很旧了,但是老实说,给出的答案都没有用.我认为,您可以使用:not 过滤器过滤元素,就像filter(':not()')一样.

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()').

Joel Potter 所述,使用$("span[class='apple']").hide();将仅选择具有一个类名 apple 的跨度.如果存在多个类(很有可能),那么这种方法将行不通.

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.

如果您点击一个单词,例如 pears ,然后可以过滤掉包含类pears的元素.

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();

您已经完成了;)

这篇关于jQuery:通过类过滤掉元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆