说我有两个
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.