JQuery基于 id隐藏

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

例如,让我们说这是我的表:

row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

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

考虑到页面上的ID必须是唯一的,因此我建议您使用css类而不是ID

HTML

 
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

JS

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

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

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

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

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

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

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

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

试试这个…

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

看到这个……

DEMO

试试这个:

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

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

尝试

 
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

然后

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

演示: 小提琴

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