如何从jQuery选择器中排除$(this)?
我有这样的事情:
单击其中一个链接时,我想在未单击的链接上执行.hide()函数。 我理解jQuery有:not selector,但在这种情况下我无法弄清楚如何使用它,因为我必须使用$(".content a")
选择链接
我想做点什么
$(".content a").click(function() { $(".content a:not(this)").hide("slow"); });
但在这种情况下我无法弄清楚如何正确使用:not selector。
尝试使用not()
方法而不是:not()
选择器 。
$(".content a").click(function() { $(".content a").not(this).hide("slow"); });
您可以使用not
函数而不是:not
selector:
$(".content a").not(this).hide("slow")
您还可以使用jQuery .siblings()
方法:
HTML
使用Javascript
$(".content").on('click', 'a', function(e) { e.preventDefault(); $(this).siblings().hide('slow'); });
工作演示: http : //jsfiddle.net/wTm5f/
您应该使用“siblings()”方法,并且为了应用该效果而阻止反复运行“.content a”选择器:
HTML
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/