以下是可以正常工作的虚拟代码。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<a class="navbar-brand" href="#" style="display: inline-block; width:250px;">Logo Image</a>
<ul class="navbar-nav" id="sideNav">
<li id="campaignsNav">
<a class="nav-link" href="#" style="margin-left:30px;"><strong>Campaigns</strong></a>
</li>
<li id="subscribersNav">
<a class="nav-link" href="#" style="margin-left:30px;"><strong>Subscribers</strong></a>
</li>
<li id="reportsNav">
<a class="nav-link" href="#" style="margin-left:30px;"><strong>Reports</strong></a>
</li>
</ul>
</nav>
<div id="wrapper">
<div id="campaignsPage">
<p>This is campaign Page</p>
</div>
<div id="subscribersPage">
<p>This is subscribers page</p>
</div>
<div id="reportsPage">
<p>MUSIC PAGE reports</p>
</div>
</div>
</div>
$('#campaignsPage, #subscribersPage, #reportsPage').hide();
$("#sideNav li").each(function(i) {
$(this).click(function() {
$("#wrapper").find("div:eq('" + i + "')").show().siblings().hide();
});
});
但是当我在div中添加实际内容时:
<div id="campaignsPage">
<p>My actual code with a form and multiple container to hold other information.</p>
</div>
上述工作代码停止,并且每次点击菜单项时都会显示不同的内容停止。任何人都可以建议我在哪里做错了。
答案 0 :(得分:1)
问题在于:
$("#wrapper").find("div:eq('" + i + "')").show().siblings().hide();
find
在深度优先遍历(文档顺序)中查看所有后代。只有你的模板,这很好,但当你把你的真实内容放入(大概是div
s)时,你就搞砸了。
您可以使用.children
而不是.find
进行修复。例如:
$('#campaignsPage, #subscribersPage, #reportsPage').hide();
$("#sideNav li").each(function(i) {
$(this).click(function() {
$("#wrapper").children("div:eq('" + i + "')").show().siblings().hide();
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<a class="navbar-brand" href="#" style="display: inline-block; width:250px;">Logo Image</a>
<ul class="navbar-nav" id="sideNav">
<li id="campaignsNav">
<a class="nav-link" href="#" style="margin-left:30px;"><strong>Campaigns</strong></a>
</li>
<li id="subscribersNav">
<a class="nav-link" href="#" style="margin-left:30px;"><strong>Subscribers</strong></a>
</li>
<li id="reportsNav">
<a class="nav-link" href="#" style="margin-left:30px;"><strong>Reports</strong></a>
</li>
</ul>
</nav>
<div id="wrapper">
<div id="campaignsPage">
This is campaign Page
<div>I'm a div</div>
<div>I'm another div</div>
</div>
<div id="subscribersPage">
<p>This is subscribers page</p>
</div>
<div id="reportsPage">
<p>MUSIC PAGE reports</p>
</div>
</div>
</div>
&#13;
那就是说,我不会这样做。您已经通过id
(campaignsNav
控件campaignsPage
等)将这些部分联系起来了,所以我可能只是将它们联系起来:
$("#sideNav li").click(function() {
var target = "#" + this.id.replace(/Nav$/, "Page");
$(target).siblings().hide().end().show();
});
示例:
$('#campaignsPage, #subscribersPage, #reportsPage').hide();
$("#sideNav li").click(function() {
var target = "#" + this.id.replace(/Nav$/, "Page");
$(target).siblings().hide().end().show();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<a class="navbar-brand" href="#" style="display: inline-block; width:250px;">Logo Image</a>
<ul class="navbar-nav" id="sideNav">
<li id="campaignsNav">
<a class="nav-link" href="#" style="margin-left:30px;"><strong>Campaigns</strong></a>
</li>
<li id="subscribersNav">
<a class="nav-link" href="#" style="margin-left:30px;"><strong>Subscribers</strong></a>
</li>
<li id="reportsNav">
<a class="nav-link" href="#" style="margin-left:30px;"><strong>Reports</strong></a>
</li>
</ul>
</nav>
<div id="wrapper">
<div id="campaignsPage">
This is campaign Page
<div>I'm a div</div>
<div>I'm another div</div>
</div>
<div id="subscribersPage">
<p>This is subscribers page</p>
</div>
<div id="reportsPage">
<p>MUSIC PAGE reports</p>
</div>
</div>
</div>
&#13;
其他选项包括data-*
属性等。