假设我们有表:
<table>
<tr>first</tr>
<tr class="ClassName">second</tr>
<tr class="ClassName">third</tr>
</table
我们想要删除带有 class='ClassName'
的行
我写了一段代码:
var Ids = $('.ClassName').map(function () {
return this
}).get();
if (Ids.length > 0) {
for (var i; i = 0; i++) {
Ids[i].hide();
}
}
我需要知道该类是否有任何行。如果是的话我需要删除它们。如果没有,我调用 ajax,从数据库读取并将行添加到表中。它是显示/隐藏机制的一部分
但是我的代码什么也没做。行仍然出现在页面中。我应该改变什么?
最佳答案
jQuery
// check if are any rows with this class
var $rows = $('table tr.ClassName');
if( $rows.length > 0 ) {
// remove them or hide with .hide();
$rows.remove();
} else {
// call ajax ...
}
关于javascript - 从表中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22959970/