假设我有以下内容:
var a = $("#a");
var b = $("#b");
//I want to do something as such as the following :
$(a,b).click(function () {/* */}); // <= does not work
//instead of attaching the handler to each one separately
显然,上述方法不可行,因为在
$
函数中,第二个参数是context
,而不是另一个元素。
那么我该如何一次性将事件附加到这两个元素上呢?
[更新]
peirix发布了一个有趣的代码片段,其中他使用&
符号组合元素。但我注意到了这个问题:
$(a & b).click(function () { /* */ }); // <= works (event is attached to both)
$(a & b).attr("disabled", true); // <= doesn't work (nothing happens)
从上面可以看到,显然只有在附加事件时使用&
符号的组合才有效吗?