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();
});
精彩评论