HTML/CSS How to make a menu closed and then use class=“active”


Tags: javascript,html,css,menu

Problem :

I want to make a menu closed and when I visite a URL the <li> should change his class to class="active" but at the beginning I want to that class stay without class="active".

And I wonder which way is the best to do this Javascript or it can do it just with css.



Solution :

Best way with jQuery

$(document).ready(function(){
var str=location.href.toLowerCase();
$(".navigation li a").each(function() {
if (str.indexOf(this.href.toLowerCase()) > -1) {
 $("li.active").removeClass("active");
$(this).parent().addClass("active");
}
 });
 })

    CSS Howto..

    How to fix content leaks out of an element despite use of “overflow:hidden”?

    How can I :hover over different links in one line while getting the spacing correct?

    How to match element containing another element?

    Css When having 2 bg-image, how to have 1 to extend out of the grid

    How to position footer correctly with css?

    how to align divs vertically in html and css?

    How to do proper and responsive Image Grid view with display:flex [duplicate]

    how to tag multiple elements? [closed]

    How to set alternate row color for a given attached diagram

    CSS How to make a DIV that starts 100px from the top reach the bottom of the page

    How to make “button-like appearance” with CSS

    How to force div element to keep its contents inside container

    How to make a div 100% width minus a certain amount?

    How to select two different elements with an XPath query

    How do you deploy a project using Less CSS to Heroku?

    How to horizontally center Twitter Bootstrap grid elements

    How do I write a php function to show class in navigation menu url using php self

    How can I use Perl to get a list of CSS elements with a color or background color attribute?

    CSS how to translate an image up, and have the div adjust to the new size?

    How to apply css using ng-class in angularjs

    How to scale jumbotron bootstrap on larger monitors?

    HTML/CSS: How to move my navigation bar to the middle?

    how to bring ul element to the center of the div? CSS

    How can i get the source of the image in one div to another?

    How to wrap a DIV around a DIV with CSS

    how to: dynamically switch css page

    Prevent div from showing after slideUp() - jQuery

    How to Make Tabs In CSS?

    How to do multiple class selectors in Stylus CSS pre-processor

    How to set a width of the background (HTML and CSS)