将事件处理程序绑定到多个元素jQuery? [英] Bind an event handler to multiple elements jQuery?

查看:108
本文介绍了将事件处理程序绑定到多个元素jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一些实验,但似乎没有使用jQuery将一个事件处理程序成功绑定到多个元素。这是我试过的:

  $('selector1','selector2')bind('click',function ){
$('someSelector')。removeClass('coolClass');
});

我已经测试了我所有的选择器,它们都是有效的。



我甚至想做甚么?如果是这样,我可以使用 .live()吗?



谢谢!

解决方案

要澄清,我们将选择器字符串解压缩成一个变量:

  var selector = ['selector1','selector2']; 

以上与您所写的相似。

  var selector ='selector1,selector2'; 

这是使用界面的正确方法。请注意,它是单个字符串中的逗号分隔的选择器列表。

  $('selector1,selector2')。bind ...)


I've done some experimenting, but can't seem to successfully bind one event handler to multiple elements using jQuery. Here's what I've tried:

$('selector1', 'selector2').bind('click', function() {
        $('someSelector').removeClass('coolClass'); 
});

I've tested all my selectors, and they are all valid.

Is what I'm trying to do even possible? If so, can I do it with .live() as well?

Thanks!

解决方案

To clarify let us extract the selector string into a variable:

var selector = ['selector1', 'selector2'];

the above is similar to what you have written.

var selector = 'selector1, selector2';

this is the correct way to use the interface. Note that it is a comma separated list of selector in a single string.

$('selector1, selector2').bind(...)

这篇关于将事件处理程序绑定到多个元素jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆