如何从jQuery选择器中排除$(this)?

时间:2021-01-08 15:01:27

I have something like this:

我有这样的事情:

<div class="content">
    <a href="#">A</a>
</div>
<div class="content">
    <a href="#">B</a>
</div>
<div class="content">
    <a href="#">C</a>
</div>

When one of these links is clicked, I want to perform the .hide() function on the links that are not clicked. I understand jQuery has the :not selector, but I can't figure out how to use it in this case because it is necessary that I select the links using $(".content a")

单击其中一个链接时,我想在未单击的链接上执行.hide()函数。我理解jQuery有:not selector,但在这种情况下我无法弄清楚如何使用它,因为我必须使用$(“。content a”)选择链接

I want to do something like

我想做点什么

$(".content a").click(function()
{
    $(".content a:not(this)").hide("slow");
});

but I can't figure out how to use the :not selector properly in this case.

但在这种情况下我无法弄清楚如何正确使用:not selector。

4 个解决方案

#1


356  

Try using the not() method instead of the :not() selector.

尝试使用not()方法而不是:not()选择器。

$(".content a").click(function() {
    $(".content a").not(this).hide("slow");
});

#2


41  

You can use the not function rather than the :not selector:

您可以使用not函数而不是:not selector:

$(".content a").not(this).hide("slow")

#3


9  

You can also use the jQuery .siblings() method:

您还可以使用jQuery .siblings()方法:

HTML

<div class="content">
  <a href="#">A</a>
  <a href="#">B</a>
  <a href="#">C</a>
</div>

Javascript

$(".content").on('click', 'a', function(e) {
  e.preventDefault();
  $(this).siblings().hide('slow');
});

Working demo: http://jsfiddle.net/wTm5f/

工作演示:http://jsfiddle.net/wTm5f/

#4


5  

You should use the "siblings()" method, and prevent from running the ".content a" selector over and over again just for applying that effect:

您应该使用“siblings()”方法,并且为了应用该效果而阻止反复运行“.content a”选择器:

HTML

<div class="content">
    <a href="#">A</a>
</div>
<div class="content">
    <a href="#">B</a>
</div>
<div class="content">
    <a href="#">C</a>
</div>

CSS

.content {
    background-color:red;
    margin:10px;
}
.content.other {
    background-color:yellow;
}

Javascript

$(".content a").click(function() {
  var current = $(this).parent();
  current.removeClass('other')
    .siblings()
    .addClass('other');
});

See here: http://jsfiddle.net/3bzLV/1/

见这里:http://jsfiddle.net/3bzLV/1/

#1


356  

Try using the not() method instead of the :not() selector.

尝试使用not()方法而不是:not()选择器。

$(".content a").click(function() {
    $(".content a").not(this).hide("slow");
});

#2


41  

You can use the not function rather than the :not selector:

您可以使用not函数而不是:not selector:

$(".content a").not(this).hide("slow")

#3


9  

You can also use the jQuery .siblings() method:

您还可以使用jQuery .siblings()方法:

HTML

<div class="content">
  <a href="#">A</a>
  <a href="#">B</a>
  <a href="#">C</a>
</div>

Javascript

$(".content").on('click', 'a', function(e) {
  e.preventDefault();
  $(this).siblings().hide('slow');
});

Working demo: http://jsfiddle.net/wTm5f/

工作演示:http://jsfiddle.net/wTm5f/

#4


5  

You should use the "siblings()" method, and prevent from running the ".content a" selector over and over again just for applying that effect:

您应该使用“siblings()”方法,并且为了应用该效果而阻止反复运行“.content a”选择器:

HTML

<div class="content">
    <a href="#">A</a>
</div>
<div class="content">
    <a href="#">B</a>
</div>
<div class="content">
    <a href="#">C</a>
</div>

CSS

.content {
    background-color:red;
    margin:10px;
}
.content.other {
    background-color:yellow;
}

Javascript

$(".content a").click(function() {
  var current = $(this).parent();
  current.removeClass('other')
    .siblings()
    .addClass('other');
});

See here: http://jsfiddle.net/3bzLV/1/

见这里:http://jsfiddle.net/3bzLV/1/