• All geek questions in one place

    Css added with addclass not working on page load

    I have a menu with the following links.

    <div class="menu"> <ul> <li><a href="aboutus.php" onclick="changecss(1)">About Us</a></li> <li><a href="services.php" onclick="changecss(2)">Services</a></li> <li><a href="career.php" onclick="changecss(3)">Career</a></li> <li><a href="contactus.php" onclick="changecss(4)">Contact Us</a></li> </ul> </div> 

    I want the current page link to be active in the menu. Therefore, I use the addclass method.

     $(".menu ul li a").click(function() { $(".menu ul li a").removeClass('selected'); $(this).addClass('selected'); }); 

    All of these codes are on the header.html page, which is my header file. I am including this page using php on all other pages.

    The problem is that when I click on the menu links addclass works, but by the time the page loads (on the href page) addclass's css will not work. Can anyone help solve the problem?

    +4
    jquery css
    Jenz May 22, '13 at 6:09
    source share
    4 answers

    Thanks to everyone.

    Solved the problem. I have included addclass on all pages, otherwise it will not work on loading.
    In the header.php file, I assigned the current page URL to the page variable so that on any pages that contain the header file, the page variable is available.

      $(document).ready(function(){`<br/> if(page=="localhost/workspace/pjt/aboutus") {` <br/> $(".menu ul li a").removeClass('selected');` <br/> $(".menu ul li a#about").addClass('selected'); } }); 
    +1
    Jenz May 23 '13 at 11:31
    source share

    Do you use a document method? If not, this is your problem. The DOM does not load fully when you execute jQuery code. Run your code as follows:

     $(function(){ $(".menu ul li a").click(function() { $(".menu ul li a").removeClass('selected'); $(this).addClass('selected'); }); }); 

    ... or ... just execute your code in the script tag at the bottom of the page (right before the closing tag).

    Like the side of node:

     $(function() {}); 

    not suitable for

     $(document).ready(function(){ ... }); 
    +5
    Bas slagter May 22, '13 at 6:14
    source share

    Try under the document. Already like

     $(document).ready(function(){ $(".menu ul li a").click(function() { $(".menu ul li a").removeClass('selected'); $(this).addClass('selected'); }); //Here defaulty add class for first link on page load like $(".menu ul li a:eq(0)").addClass('selected'); }) 
    +2
    Gautam3164 May 22, '13 at 6:15
    source share

    It is better to do this in server-side code, but it is possible to do this on the client side. You should get the current page address, and then select your link based on this:

     $(function () { // get the current page var link = window.location.href.split('/'); page = link[link.length - 1]; // now select the link based on the address $('.menu ul li a[href*="' + page + '"]').addClass('selected'); }); 
    0
    user1823761 May 22 '13 at 6:31
    source share

    Source: https://habr.com/ru/post/1482093/

    More articles:

    • Deny order in gRaphael pie chart - javascript
    • How to get last hour, last month and yesterday in javascript? - javascript
    • Android - Get color from ImageView with ColorFIlter used for drawing - android
    • How can I create editor templates for Nullable as well as DateTime? - asp.net-mvc
    • 500: Internal server error when using PHPMailer - php
    • Non Public API Usage and Unique Identification Methods UIDevice iOS - ios
    • Check if the matrix is ​​SPD - matlab
    • NHibernate LINQ: Why is the query using the wrong char value if I repeat the call? - nhibernate
    • how to stop a stopwatch while debugging C # and vs2012 - c #
    • how to show iCarousel center view only selected other closest view show some dark - ios

    All Articles

    Geek Questions | 2019