简体   繁体   中英

Page can't load after add/removeclass

i got some problem when i was addclass and removeclass using jquery on my menu the problem is the page can't be load...

this my html :

<div class="menu-gila-container">
  <ul id="menu-gila" class="menu nav-menu">
    <li class="current-menu-item"><a href="home">HOME</a></li>
    <li><a href="html">HTML</a></li>
    <li><a href="css">CSS</a></li>
    <li><a href="php">PHP</a></li>
    <li><a href="mysql">MYSQL</a></li>
    <li><a href="gaze">GA TAU</a></li>
    <li><a href="gaze">GA TAU</a></li>
    <li><a href="diary">DIARE</a></li>
    <li><a href="ask">ASK ME</a></li>
    <li><a href="about">ABOUT</a></li>
  </ul>
</div>  

This my javascript :

<script>
$(document).ready(function() {
    $("ul.nav-menu li").click(function(e) {
        e.preventDefault();

        //Remove the active class
        $("ul.nav-menu li").removeClass("current-menu-item");

        //Add the active tab to the selected tab
        $(this).addClass("current-menu-item"); 
    }); 
}); 
</script>

Please help me

Remove this line e.preventDefault(); it stops

OR

$(document).ready(function () {
    $("ul.nav-menu li").click(function (e) {
        e.preventDefault();
        $("ul.nav-menu li").removeClass("current-menu-item"); //Remove the active class
        $(this).addClass("current-menu-item"); //Add the active tab to the selected tab
        window.location.href = $(this).attr('href')
    });
});

Add Active Navigation Class Based on URL

Example

HTML

    <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/about/">About</a></li>
        <li><a href="/clients/">Clients</a></li>
        <li><a href="/contact/">Contact Us</a></li>
    </ul>

Script

$(function() {
  $('ul.nav-menu li a[href^="/' + location.pathname.split("/")[1] + '"]').prev().addClass('active');
});

Try this:

$(document).ready(function() {
    $("ul.nav-menu li").click(function(e) {
        e.preventDefault();
        $(this).siblings('li').removeClass('current-menu-item');
        $(this).addClass('current-menu-item');
    }); 
}); 

Demo here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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