javascript jquery - 在每个元素上运行函数,除了被点击的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4445952/
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-25 11:28:20  来源:igfitidea点击:

jquery - run function on each element except the one that was clicked

javascriptjquery

提问by Matthew

As some example code, I might have something like this:

作为一些示例代码,我可能有这样的事情:

$('a.parent').click(function(){

        $('a.parent').each(function(){
            $(this).stop(true,false).animate({
                width: '140px'
            },200,function(){
            });
        });

        $(this).animate({
            width: '160px'
        },200,function(){
        });

    });

The problem is that I don't want the element that was clicked to animate to 140px width and then back to 160px.

问题是我不希望被点击的元素动画到 140 像素宽度然后回到 160 像素。

Is there a way to run 'each' on only the elements in a set who were not clicked? Or is there a better way?

有没有办法只对集合中未被点击的元素运行“每个”?或者,还有更好的方法?

回答by hakim-sina

you can use :not(this) so change :

你可以使用 :not(this) 所以改变:

 $('a.parent').each(function(){
            $(this).stop(true,false).animate({
                width: '140px'
            },200,function(){
            });
        });

to :

到 :

$('a.parent:not('+this+')').each(function(){
            $(this).stop(true,false).animate({
                width: '140px'
            },200,function(){
            });
        });

or use .not(this) after $('a.parent') , so :

或在 $('a.parent') 之后使用 .not(this) ,所以:

$('a.parent').not(this).each(function(){
                $(this).stop(true,false).animate({
                    width: '140px'
                },200,function(){
                });
            });