我有这样的东西:

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

当单击其中一个链接时,我想对未单击的链接执行.hide()函数。我知道jQuery有:not选择器,但我不知道在这种情况下如何使用它,因为有必要使用$(")选择链接。内容“)

我想做一些

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

但我不知道在这种情况下如何正确使用:not选择器。


尝试使用not()方法代替:not()选择器。

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

你可以使用not函数而不是:not选择器:

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

你也可以使用jQuery .sibling()方法:

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');
});

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


您应该使用“sibling()”方法,并防止运行“。contenta“选择器一遍又一遍地应用这个效果:

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');
});

请看这里:http://jsfiddle.net/3bzLV/1/