98

进阶题,简单题:

如何在 jQuery 中执行以下操作(隐藏除 之外的所有内容$(this))?

$("table tr").click(function() {
    $("table tr:not(" + $(this) + ")").hide();
    // $(this) is only to illustrate my problem

    $("table tr").show();
});
4

4 回答 4

193
$(this).siblings().hide();

穿越/兄弟姐妹

于 2009-08-25T13:37:18.203 回答
159
$("table.tr").not(this).hide();

顺便说一句,我认为您的意思是$("table tr")(用空格而不是点)。
您拥有它的方式是,它选择每个具有类tr(例如,<table class="tr">)的表,这可能不是您想要的。

有关详细信息,请参阅文档

于 2009-08-25T13:50:22.633 回答
6

如果你想将 not() 与其他一些选择器结合起来,你可以使用 add():

$('a').click(function(e){
  $('a').not(this).add('#someID, .someClass, #someOtherID').animate({'opacity':0}, 800);
});

这将淡出所有其他链接,但点击的链接,另外淡出一些选定的 id 和类。

于 2014-02-07T12:50:55.137 回答
0

我认为一个解决方案可以是这样的:

$("table.tr").click(function() {
    $("table.tr:not(" + $(this).attr("id") + "").hide(); // $(this) is only to illustrate my problem
    $(this).show();
})

--编辑评论:

$("table.tr").click(function() {
    $("table.tr:not(#" + $(this).attr("id") + ")").hide(); // $(this) is only to illustrate my problem
    $(this).show();
})
于 2009-08-25T13:48:32.777 回答