我一次加载页面上的所有内容。选择树后,我只想显示 1 个网格。我的代码所做的不是完全摆脱隐藏的网格,也不是将选定的网格移动到顶部(我认为可能是因为隐藏的网格没有完全消失)
下面的代码似乎隐藏了所有内容,然后使“gridWrapper2”保持可见,但隐藏了其中的表格。
如何修改以下代码行以隐藏除指定 id 中的内容之外的所有内容。换句话说,不要隐藏嵌套在“gridWrapper2”中的任何标签
$('#contents div').not("#gridWrapper2").hide();
这里是我使用代码的地方
$(function() {
$("span.dynatree-edit-icon").live("click", function(e) {
alert("Edit " + $.ui.dynatree.getNode(e.target));
});
$("#tree").dynatree({
onActivate: function(node) {
$('#contents div').not("#gridWrapper2").hide();
$("#info").text("You activated " + node);
},
children: [{
title: "Item 1"
}, {
title: "Folder 2",
isFolder: true,
children: [{
title: "grid2"
}, {
title: "grid"
}]
}, {
title: "Item 3"
}]
});
<body>
<div class="container-fluid text-left">
<div class="row content">
<div class="col-sm-2 sidenav" id="tree">
</div>
<div class="col-sm-8" id="contents">
<div id="gridWrapper"> <table id="grid"></table> </div>
<div id="gridWrapper2"> <table id="grid2"></table> </div>
</div>
<div id="info"> </div>
</div>
</div>
</div>
</body>
</html>
最佳答案
确保表格内的内容是正确/有效的 html。有关更多信息,请参见下面的示例。 display:none
将应用于 table
标记,并且只有其中有效的 html 可以被隐藏,如果无效,其他所有内容都会出现。
$(function() {
$("#tree").click(function() {
$('#contents table').not("#grid2").not("#grid4").hide();
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid text-left">
<div class="row content">
</div>
<div class="col-sm-8" id="contents">
<table id="grid">
<tr>
<td>grid</td>
</tr>
</table>
<table id="grid2">
<tr>
<td>grid2</td>
</tr>
</table>
<table id="grid3">grid3</table>
<table id="grid4">grid4</table>
</div>
<div id="info"></div>
</div>
</div>
<button id="tree">Tree</button>
关于javascript - 在选择树时隐藏 Div 中除一个元素外的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36748483/