开发者

How to use 2 classes for the same function with jQuery

How do I run the same click function for 2 classes ?

I 开发者_StackOverflow中文版have this :

$('.buythis').click(function(){ blabla }

And I want to use the same line for a different class like :

    $('.anotherclass').click(function(){ blabla }

Now ... how can I use the same click function without redeclaring it ? i want something like :

          ($('.buythis'),$('.anotherclass')).click(function(){ blabla }


Try this:

 $('.buythis, .anotherclass').click(function(){ blabla }


Use a comma between the two selectors:

$('.buythis, .anotherclass').click( ... );

or use two selectors chained with .add().

$('.buythis').add('.anotherclass').click( ... );

The latter syntax can be useful if the selectors are complicated since it can remove ambiguity from the selector parser and make the code easier to read.


Using multiple selector1:

$('.buythis, .anotherclass').click(function(){ blabla });


Then use following code :

$('.buythis, .anotherclass').click(function(){ blabla }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜