javascript - Jquery 选择器 - 多个项目

标签 javascript jquery css-selectors

假设我有两个 jquery 选择:

var txtA = $('#txtA');
var txtB = $('#txtB');

为了附加相同的事件函数,这是最简洁的方法吗,还是我错过了一段明显的语法或 jquery 技巧?

$(jQuery.merge(txtA , txtB )).click(function () { alert('hello'); });

谢谢。

最佳答案

.add()应该这样做(前提是您已经填充了 txtAtxtB 并希望重用这些选择。):

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/

相关文章:

Javascript在网页上执行js,例如chrome控制台

javascript - 如何获得 iframe 内容宽度?

jQuery 如何实现延迟滑动?

javascript - CasperJS 无法使用 CSS 选择器或 xPath 找到元素

javascript - GreaseMonkey @include 约 :newtab

javascript - CSS 导航栏覆盖 Bootstrap 模式

javascript - 在内容的开头和结尾添加 div

javascript - 无法从 Firebase 下载数据 - TypeError : undefined is not an object

javascript - 通过未显式设置的 css 属性查找元素

internet-explorer-8 - ie8中的YUI3选择器问题