简体   繁体   English

活动类别附加在 <li> jQuery不适用于页面加载

[英]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; }

See the documentation here . 请参阅此处的文档。

here what is happening - 这是怎么回事-

  • when you use "e.preventDefault();" 当您使用“ e.preventDefault();”时 for click event of a link you are actually stopping it from carrying out its default task which is - "loading a link on clicking",yet the javascript is carried out so your "active class" is applied on clicking but the link doesn't work and page doesn't load. 对于链接的click事件,您实际上是在阻止它执行其默认任务-“在单击时加载链接”,但是javascript已执行,因此您的“活动类”在单击时适用,但链接没有工作和页面无法加载。
  • when you remove "e.preventDefault(); the link does work it loads the page its linked to but active class won't be applied because its totally a new page which is loaded which has nothing to do with javascript on your previous page from where it is loaded".. 当您删除“ e.preventDefault();链接确实起作用时,它会加载与其链接的页面,但不会应用活动类,因为它完全是一个新页面,该页面与您上一页中的javascript没有关系它的加载位置”。

hope this helps in solving your problem.. 希望这有助于解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM