使用jQuery隐藏表中显示的所有子元素



当我对表应用搜索时,我试图隐藏表行的所有显示的子级。但我做不到。这是我的代码。不知道怎么了。

var table = $("#tblTemplateList").DataTable();
$("#tblTemplateList tr").each(function(index,tr){
if($(tr).child.isShown()){
$(tr).child.hide();
}
});

这个小例子

function hiderows() {
$("#tblTemplateList tr").each(function(index, tr) {
if ($(tr).is(":visible")) {
$(tr).hide();
} else {
$(tr).show();
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tblTemplateList">
<tr><td>row1</td></tr>
<tr><td>row2</td></tr>
</table>
<button type="button" onclick="hiderows()">
Hide/Show Rows
</button>

可能有助于

最新更新