我使用此代码根据条件隐藏表第六列中的元素(我的意思是如果表的跨度第四列的文本为“0”)。
但是此代码仅适用于表格的第一行。
我如何对目标表的所有行执行此功能?
if ($('#table tr td:eq(4) > span').text() == "0") {
$('#table tr td:eq(6) > .PrintReport').hide();
}
最佳答案
如果您可以发布 tr
的完整 HTML 结构,那么您将获得更优化的解决方案。查看您现有的代码,您可以执行以下操作:
$('#table tr').each(function() {
var text = $('td:eq(4) > span', this).text();
$('td:eq(6) > .PrintReport', this).toggle(text != '0');
});
请注意,在循环内我使用 this
作为选择器中的上下文。
编辑:解释上面的一些代码 -
//This runs the selector in the context of 'this' (the table row)
//It is functionally equivalent to $(this).find('td:eq(6) > .PrintReport')
$('td:eq(6) > .PrintReport', this)
//This will .show() it if the expression evaluates to true
//and hide if false
.toggle(text != '0')
关于jquery - 为表 jQuery 的所有行运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6278261/