开发者

jquery - how could I bind the same things to 2 diffrent classes?

I have got:

$('.class1').click(function(){   //do
things on $(this) element })

and

$('.class2').click(function(){
  //do things on $(this) element
})

things to do in each of that is the same, but when I did:

$('.class2').click(function(){
   $('.class1').click();
})

... it was disaster, that actived开发者_Go百科 each element with class1. Is there any similar solution to above one?


Try this:

$('.class1, .class2').click(function(){   
  //do things on $(this) element 
}) ;


$(".class1,.class2").click(function(){ });


Using the multiple selector is the best way here. However in some situations, you have to bind the handlers dynamically. Use named functions then:

function handler() {
    // something
}

$('.class1').click(handler);
$('.class2').click(handler);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜