javascript 仅从 jquery 选择中删除第一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9120707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:39:40  来源:igfitidea点击:

Remove only the first element from a jquery selection

javascriptjquery

提问by dynamic

<div class="box"></div>
<div class="box"></div> <!-- Hide -->
<div class="box"></div> <!-- Hide -->
<div class="box"></div> <!-- Hide -->

I need to hide all this div but not the first div.

我需要隐藏所有这些 div 但不是第一个 div。

I could do something like this:

我可以做这样的事情:

jQuery('.box').hide();
jQuery('.box').first().show();

Is there a way to remove the first .boxfrom the array before .hide()em?

有没有办法.box.hide()em之前从数组中删除第一个?

回答by zch

jQuery('.box').slice(1).hide()

回答by Ryan Mulready

jQuery('.box:not(:first)').hide();

http://jsfiddle.net/8wMFc/

http://jsfiddle.net/8wMFc/

回答by Rafay

try

尝试

jQuery('.box').not(':first').hide();

comparison:

比较:

@T.J.Crowder is right the code i have suggested does the extra parsing that can be avoided by .sliceas suggested by @zch

@TJCrowder 是正确的,我建议的代码执行了.slice@zch 建议的可以避免的额外解析

HEREis the profile of my code (0.8ms)and HEREis the profile of @zch's code (0.53ms)see the difference

这里是我的代码的配置文件(0.8ms)这里@zch代码的配置文件(0.53ms)查看差异

回答by Patrickdev

jQuery('.box').not(':eq(0)').hide();

That said, I prefer Residual Envy's solution.

也就是说,我更喜欢 Residual Envy 的解决方案。

回答by Royi Namir

try :

尝试 :

jQuery('.box:gt(0)').hide();