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

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

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

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

有没有办法只在一组中没有点击的元素上运行’each’? 或者,还有更好的方法?

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

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

至 :

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

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

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