如何以更简单的方式检查多个 div 的可见性?
目前我正在使用下面的代码,效果很好。
$("#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel").hide();
有优化的代码吗? (我不能使用同一类) 我的意思是如何正确使用 :visible
?
Currently i'm using below code which works well.
$("#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel").hide();
any optimised code? (i cant use same class)
i mean how to use :visible
rightly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是实现你想要的目标的方法。您正在正确有效地使用选择器。
如果您维护一个需要隐藏的标签的 ID 数组,然后动态构建选择器以通过 ID 进行查找,可以使速度更快一些。 (即使选择器可能更长,这也会更快。通过 ID 选择非常快。)
但是,不需要优化,不是吗?我们正在谈论从闪电速度到双闪电速度。一个超级 jQuery 专业人士只会做你所做的事情。
That is the way to achieve what you're going for. You are using the selectors correctly and efficiently.
You could make it a little faster if you maintained an array of the ID's of the tags that need to be hidden, and then construct your selector dynamically to find by ID. (This would be faster even though the selector might be longer. Selecting by ID is very fast.)
But, the optimization is not needed, is it? We're talking about going from lightening fast to double lightening fast. A super-duper jQuery pro would just do what you've done.
该代码看起来很完美;您正在正确使用 :visible 。
如果您想确切了解,可以查看 jQuery :visible 选择器帮助页面它是如何工作的,但简而言之,它选择可见元素 =)
That code seems perfect; you are using :visible correctly.
You can take a look at the jQuery :visible selector help page if you want to know exactly how it works, but in a few words it selects visible elements =)
好吧,我能想到的是:
$('[id$="AtBaseLevel"]:visible').hide();
这将匹配 ID 以
AtBaseLevel 结尾的任何元素
。请注意,更短并不意味着更快,因为 ID 查找的速度差不多是最快的。基于属性的选择器并没有那么优化。Well, all I can think of, is:
$('[id$="AtBaseLevel"]:visible').hide();
That would match any element whose ID ends in
AtBaseLevel
. Mind you, shorter does not mean faster, since ID lookups are about as fast as it gets. Attribute-based selectors are not that optimised.你可以这样做:
但是,这会导致所有内容都被隐藏,在隐藏元素上调用
.hide()
很好,没有任何问题,所以可能是这样的:You can do it like this:
However, this results in everything being hidden, calling
.hide()
on a hidden element is fine, nothing wrong there, so it could just be this: