[英]Active class appended on <li> with Jquery not working on page load
I have set an active class on a dynamically created Wordpress navigational menu using this Jquery: 我已经使用此Jquery在动态创建的Wordpress导航菜单上设置了一个活动类:
$(document).ready(function () {
$('.left-nav ul > li').click(function (e) {
e.preventDefault();
$('.left-nav ul > li').removeClass('active');
$(this).addClass('active');
});
});
CSS: CSS:
.left-nav ul li a {
color:#4D9120;
display:block;
}
.left-nav ul li:hover a {
color:#fff;
}
.left-nav ul li:hover {
background:#4D9120;
}
.left-nav .active{
background-color:#FF0000 !important;
}
.left-nav .active a{
color:#fff !important;
}
HTML/PHP HTML / PHP
<ul>
<?php wp_nav_menu(array('theme_location'=>'sidemenu','container'=>'','menu_id' => '','fallback_cb'=> false)); ?>
</ul>
This is the HTML final output which are generated dynamically: 这是动态生成的HTML最终输出:
<div class="col-xs-3 left-nav">
<ul>
<li><a href="#">Tutoring</a></li>
<li><a href="#">Consultancy</a></li>
<li><a href="#">Summer Camp/School</a></li>
<li><a href="#">University Applications</a></li>
<li><a href="#">Job Placements & Intenships</a></li>
</ul>
</div>
The active class is applied on clicking on any of the menu link but pages don't load. 在单击任何菜单链接时都会应用活动类,但不会加载页面。 But when I removed
e.preventDefault();
但是当我删除
e.preventDefault();
the pages load but the active class is not applied anymore. 页面加载,但不再应用活动类。
I would have loved to paste the website url but I'm currently working on a local server. 我本来希望粘贴网站网址,但目前正在本地服务器上工作。 Any help would be appreciated.
任何帮助,将不胜感激。
The current-menu-item
class should be added to the active menu item by wp_nav_menu
automatically. wp_nav_menu
自动将current-menu-item
类添加到活动菜单项中。 Get rid of the Javascript, and try adding this to your CSS: 摆脱Javascript,然后尝试将其添加到CSS中:
.left-nav ul li.current-menu-item { background-color:#FF0000 !important; }
.left-nav ul li.current-menu-item a { color:#fff !important; }
here what is happening - 这是怎么回事-
hope this helps in solving your problem.. 希望这有助于解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.