jQuery:从select器中排除$(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>  当单击其中一个链接时,我想对未被单击的链接执行.hide()函数。 我知道jQuery有:不是select器,但我不知道如何使用它在这种情况下,因为它是必要的,我select使用$(".content a") 
我想要做类似的事情
 $(".content a").click(function() { $(".content a:not(this)").hide("slow"); }); 
但我无法弄清楚如何在这种情况下使用:不正确的select。
 尝试使用not() 方法而不是:not()select器 。 
 $(".content a").click(function() { $(".content a").not(this).hide("slow"); }); 
 您可以使用not函数而不是:notselect器: 
 $(".content a").not(this).hide("slow") 
 您也可以使用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'); }); 
工作演示: http : //jsfiddle.net/wTm5f/
您应该使用“siblings()”方法,并防止运行“.content a”select器,以便应用该效果:
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/