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

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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.

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

回答1:

you can use :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).each(function(){                 $(this).stop(true,false).animate({                     width: '140px'                 },200,function(){                 });             }); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!