简体   繁体   English

如何使用jQuery或Javascript隐藏具有相同ID的所有表行?

[英]How do I hide all table rows with the same id using jQuery or Javascript?

I have a table with rows that have the of custom attribute with each other. 我有一个表,其中的行具有彼此的custom属性。 I want to hide all the table rows except for the parent row using jQuery (or Javascript). 我想使用jQuery(或Javascript)隐藏除父行以外的所有表行。 How can I go by doing this? 我该怎么做?

<table>
<tr group="1">Parent</tr>
<tr group="1">Child</tr>
<tr group="1">Child</tr>
<tr group="1">Child</tr>
</table>

Edit: Wow big typo on my part, I am terribly sorry, I meant custom attribute. 编辑:哇,我错了,我很抱歉,我的意思是自定义属性。 Updated! 更新!

they can't have the same ID, the ID tag must be unique 它们不能具有相同的ID,ID标签必须是唯一的

you could simply use: $("table tr:gt(0)").hide() 您可以简单地使用: $("table tr:gt(0)").hide()
(this only works if you don't have nested tables) (这仅在没有嵌套表的情况下有效)

With your new example, it's possible using jQuery's atribute equals selector ( here ). 在您的新示例中,可以使用jQuery的atribute equals选择器( 在此处 )。 Take a look at this tasty fiddle . 看一下这美味的小提琴

Basically, in your selector, you need this: 基本上,在选择器中,您需要这样做:

$("table tr[group='2']").hide()

Of course, this is customisable. 当然,这是可定制的。 The important bit is tr[group='2'] 重要的是tr[group='2']

EDIT 编辑

This updated fiddle should work. 这个更新的小提琴应该工作了。 If someone can post a better way, please do. 如果有人可以发布更好的方法,请这样做。

It adds to the above line with this: 它添加到上面的行:

$("table tr[group='2']").filter(":not(:first)").hide();

You should not use IDs, only use classes when an element is repeated on the page, since ID refers to be unique, which can only be used once per page. 您不应使用ID,而应在页面上重复元素时仅使用类,因为ID是唯一的,ID每页只能使用一次。 Replace the id attribute to class and then you can use this code: id属性替换为class ,然后可以使用以下代码:

$('table tr[group=1]').hide();

You can't have same id used twice on two HTML elements. 不能在两个HTML元素上使用相同的ID两次。 Furthermore, you can't start an id with a number. 此外,您不能使用数字开头ID。

If you can change your HTML markup to use classes, which would look like this: 如果您可以将HTML标记更改为使用类,则该类如下所示:

<table>
    <tr class="one">Parent</tr>
    <tr class="one">Child</tr>
    <tr class="one">Child</tr>
    <tr class="one">Child</tr>
</table>

Then, you would do it like this: 然后,您将像这样进行操作:

$('.one').hide();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM