jQuery same click event for multiple elements

Is there any way to execute same code for different elements on the page?

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

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

instead to do something like:

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

Thanks


$('.class1, .class2').on('click', some_function);

要么:

$('.class1').add('.class2').on('click', some_function);

I normally use on instead of click . It allow me to add more events listeners to a specific function.

$(document).on("click touchend", ".class1, .class2, .class3", function () {
     //do stuff
});

Hope it helps!


$('.class1, .class2').click(some_function);

确保你把一个空间像$('。class1,space here.class2'),否则它不会工作

链接地址: http://www.djcxy.com/p/2368.html

上一篇: 即时创建隐藏的表单元素

下一篇: jQuery相同的点击事件多个元素