通过 :not in jQuery 选择器隐藏除 $(this) 之外的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1328314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:14:10  来源:igfitidea点击:

Hide all but $(this) via :not in jQuery selector

jqueryjquery-selectorsthis

提问by Kordonme

Advanced title, simple question:

高级标题,简单问题:

How can I do the following in jQuery (hiding everything except $(this))?

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

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

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

回答by Alex Gyoshev

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

Traversing/Siblings

穿越/兄弟姐妹

回答by SLaks

$("table.tr").not(this).hide();


As an aside, I think you mean $("table tr")(with a space instead of a dot).
The way you have it, it selects every table which has a class of tr(eg, <table class="tr">), which is probably not what you want.

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

For more information, see the documentation.

有关更多信息,请参阅文档

回答by lenooh

If you want to combine not() with some other selectors, you can use add():

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

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

This would fadeout all other links, but the clicked one, and additionally fadeout some chosen ids and classes.

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

回答by andres descalzo

I think a solution can be this:

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

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

--EDIT for Comment:

--编辑评论:

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