假设我有两个 jquery 选择:
var txtA = $('#txtA');
var txtB = $('#txtB');
为了附加相同的事件函数,这是最简洁的方法吗,还是我错过了一段明显的语法或 jquery 技巧?
$(jQuery.merge(txtA , txtB )).click(function () { alert('hello'); });
谢谢。
最佳答案
.add()
应该这样做(前提是您已经填充了 txtA
和 txtB
并希望重用这些选择。):
txtA.add(txtB).click(function () {
alert('hello');
});
Given a jQuery object that represents a set of DOM elements, the
.add()
method constructs a new jQuery object from the union of those elements and the ones passed into the method. The argument to.add()
can be pretty much anything that$()
accepts, including a jQuery selector expression, references to DOM elements, or an HTML snippet.
关于javascript - Jquery 选择器 - 多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6896909/