jQuery JQuery根据<td> id隐藏<tr>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18233348/
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 21:15:10  来源:igfitidea点击:

JQuery hide <tr> based on <td> id

jquerytablerow

提问by BjornBogers

I would like to hide some table row from my table based on the table data id, I've only found ways to hide the table row based on the table data value but that is (in my case) not the solution.

我想根据表数据 id 从我的表中隐藏一些表行,我只找到了基于表数据值隐藏表行的方法,但这(在我的情况下)不是解决方案。

For example, lets say this is my table:

例如,假设这是我的表:

<table id='table1' border="1">
<tr id='hideme'>
<td id='letmehide'>row 1, cell 1</td>
<td id='letmehide'>row 1, cell 2</td>
</tr>
<tr id='donthideme'>
<td id='dontletmehide'>row 2, cell 1</td>
<td id='dontletmehide'>row 2, cell 2</td>
</tr>
</table>

What should my javascript/JQuery look like? Fiddle: http://jsfiddle.net/twyqS/

我的 javascript/JQuery 应该是什么样的?小提琴:http: //jsfiddle.net/twyqS/

回答by Claudio Redi

Take into account that ids must be unique on your page so I recommend you to use css classes instead of ids

考虑到 ids 在您的页面上必须是唯一的,所以我建议您使用 css 类而不是 ids

HTML

HTML

<table id='table1' border="1">
<tr class='hideme'>
<td class='letmehide'>row 1, cell 1</td>
<td class='letmehide'>row 1, cell 2</td>
</tr>
<tr class='donthideme'>
<td class='dontletmehide'>row 2, cell 1</td>
<td class='dontletmehide'>row 2, cell 2</td>
</tr>
</table>

JS

JS

$('#table1 .hideme').hide();

This would allow you to hide several rows if necessary.

这将允许您在必要时隐藏多行。

回答by Rory McCrossan

First of all is you're using jQuery, use it to attach your event handlers. It results in much more semantic HTML, and is a better separation of concerns.

首先是你正在使用 jQuery,用它来附加你的事件处理程序。它产生了更多语义 HTML,并且是更好的关注点分离。

With regard to your problem you should use the :firstselector to get the first trelement, and then the hide()function:

关于你的问题,你应该使用:first选择器来获取第一个tr元素,然后是hide()函数:

$('#table1 tr:first').hide();

Alternatively you could use the idof the tras it is intended to be unique:

或者,您可以使用 的idtr因为它旨在是唯一的:

$('#hideme').hide();

I would suggest familiarising yourself with the jQuery API, specifically the selectors available.

我建议您熟悉 jQuery API,特别是可用选择器

回答by sangram parmar

Try this:

尝试这个:

$('#table1 tr[id="hideme"]').hide();//in table1 hide tr that are with attribute id="hideme"

回答by MG_Bautista

Try this...

尝试这个...

$(document).ready(function(){
    $('button').on('click', function(){
        $('#table1 tr:first').hide();
    });
});

And see this...

看到这个...

DEMO

演示

回答by Arun P Johny

ID of an element should be unique, so use class attribute instead of ID in your elements

元素的 ID 应该是唯一的,因此在元素中使用 class 属性而不是 ID

Try

尝试

<table id='table1' border="1">
    <tr class='hideme'>
        <td class='letmehide'>row 1, cell 1</td>
        <td class='letmehide'>row 1, cell 2</td>
    </tr>
    <tr class='donthideme'>
        <td class='dontletmehide'>row 2, cell 1</td>
        <td class='dontletmehide'>row 2, cell 2</td>
    </tr>
</table>

then

然后

$('#table1 tr:has(td.letmehide)').hide()

Demo: Fiddle

演示:小提琴

回答by Dmitry Igoshin

$("#letmehide").parent().hide();

$("#letmehide").parent().hide();

回答by Simon Eric

For others who want to use the same idfor multiple trTry This:

对于想要对多个tr使用相同id 的其他人, 试试这个:

$(this).closest("tr").hide();