开发者

jQuery select 'this' and another selector at same time

开发者 https://www.devze.com 2023-01-26 21:40 出处:网络
I am using a jQuery click function and would like to know if I can use the term \'this\' to select the nav element and another selector at the same here\'s my code:

I am using a jQuery click function and would like to know if I can use the term 'this' to select the nav element and another selector at the same here's my code:

$('#nav').click(function() {
    开发者_如何学编程$(this, '#anotherSelector').hide();
});

This doesn't work. It selects the #anotherSelector and not the #nav element as well. What am I doing wrong?

Many thanks in advance.


Use .add() to add another selector into your set of elements you want to deal with, like this:

$('#nav').click(function() {
    $(this).add('#anotherSelector').hide();
});


Use $(this).add('#anotherSelector').hide(); instead.

See add

What you've got at the moment reads as "search for the DOM element (this), that is inside the element whose ID is anotherSelector."; see here for more info.


You can use the add() function to extend your selection

$(this).add('#anotherSelector').hide();


$('#nav').click(function() {
    $('#nav, #anotherSelector').hide();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消