简体   繁体   English

jquery - 在每个元素上运行函数,但单击的元素除外

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

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. 问题是我不希望被点击的元素动画为140px宽度,然后再回到160px。

Is there a way to run 'each' on only the elements in a set who were not clicked? 有没有办法只在一组中没有点击的元素上运行'each'? Or is there a better way? 或者,还有更好的方法?

you can use :not(this) so change : 你可以使用:not(this)so change:

 $('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(){
                });
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM