我怎么能用jQuery一次选择'this'和其他东西?

时间:2011-03-31 23:07:15

标签: javascript jquery jquery-selectors

假设我有一个点击处理程序:

$('some_selector').click(function(){

});

在处理程序内部,我可以使用$(this)来选择被单击的元素。

有没有办法在1个语句中选择被点击的元素和其他内容?也许,比如:

$(this, 'some_other_selector')

2 个答案:

答案 0 :(得分:9)

使用.add()

http://api.jquery.com/add/

例如

$('some_selector').click(function(){
    $(this).add('some_other_selector');
});

答案 1 :(得分:0)

而不是.add()你也可以这样做:

$([ this, $(selector)[0] ])

我更喜欢.add(),因为上述方法仅限于只返回1个元素的选择器。

它的工作原理是因为你可以将DOMElement的数组传递给jQuery。