我正在处理单页网站,并希望根据点击的菜单项显示内容。而不是每次我尝试使用JQuery实现它时加载新页面。如果有人可以帮我弄清楚我的代码错在哪里。
$('#campaignsPage, #subscribersPage, #reportsPage').hide();
$(".sideNav li").each(function(i) {
$(this).click(function() {
$("#wrapper").find("div:eq('" + i + "')").show().siblings().hide();
});
});

<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>
&#13;
期望的结果 - : 如果我点击菜单中的广告系列项目(id =&#34; campaignsNav&#34;),则div的内容为id =&#34; campaignsPage&#34;应该显示。
答案 0 :(得分:2)
您不需要迭代li来附加相同的事件。您可以绑定单击元素,使用单击的li元素id为目标元素创建id,并使用它显示隐藏目标和兄弟元素:
$("#sideNav li").click(function(i) {
var targetDivID = this.id.replace('Nav','Page');
$('#'+targetDivID).show().siblings().hide();
});
您还可以使用.first().click()
链接点击事件以触发第一个元素点击并默认显示。
$(function(){
$("#sideNav li").click(function(i) {
var targetDivID = this.id.replace('Nav','Page');
$('#'+targetDivID).show().siblings().hide();
}).first().click();
});
&#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"><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>
&#13;
答案 1 :(得分:1)
您的ul
元素的ID值为sideNav
,但您使用的是.sideNav
类选择器,应将其更改为#sideNav
的ID选择器,它会很有效:
$(document).ready(function(){
$('#campaignsPage, #subscribersPage, #reportsPage').hide();
$("#sideNav li").each(function(i) {
$(this).click(function() {
$("#wrapper").find("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"><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>
&#13;
答案 2 :(得分:1)
您必须使用.sideNav
更改#sideNav
,因为您使用id
而不是class
$('#campaignsPage, #subscribersPage, #reportsPage').hide();
$("#sideNav li").each(function(i) {
$(this).click(function() {
$("#wrapper").find("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">
<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>
&#13;