I want to highlight the current menu you have click. I\'m using CSS, but it is now working.
here is my css code:
#sub-header ul li:hover{ background-
Answer: You need CSS for “current” link here is tut.
Description of jQuery menu nav
Sample : One of meny solution
Its working for me
Following @Sampson's answer, I approached it this way -
HTML:
div
with content
class in each page, which holds the contents of that page. Header and Footer are separated.content
. For example, if I am creating a CONTACT US page, I will put the contents of the page inside <section class="content contact-us"></section>
.<body>
<header>
<div class="nav-menu">
<ul class="parent-nav">
<li><a href="#">Home</a></li>
<li><a href="#">Contact us</a></li>
...
</ul>
</div>
</header>
<section class="content contact-us">
Content for contact us page goes here
</section>
<footer> ... </footer>
</body>
CSS:
active
class, which holds the styling for an active menu..active {
color: red;
text-decoration: none;
}
<body>
<header>
<div class="nav-menu">
<ul class="parent-nav">
<li><a href="#">Home</a></li>
<li><a href="#">Contact us</a></li>
...
</ul>
</div>
</header>
<section class="content contact-us">
Content for contact us page goes here
</section>
<footer> ... </footer>
</body>
JavaScript:
content
class have the same class as menu text (lowercase and without spaces), add active
class to the menu item.var $allMenu = $('.nav-menu > .parent-nav > li > a');
var $currentContent = $('.content');
$allMenu.each(function() {
$singleMenuTitle = $(this).text().replace(/\s+/g, '-').toLowerCase();
if ($currentContent.hasClass($singleMenuTitle)) {
$(this).addClass('active');
}
});
.active {
color: red;
text-decoration: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<header>
<div class="nav-menu">
<ul class="parent-nav">
<li><a href="#">Home</a></li>
<li><a href="#">Contact us</a></li>
...
</ul>
</div>
</header>
<section class="content contact-us">
Content for contact us page goes here
</section>
<footer> ... </footer>
</body>
Why I Approached This?
body
tag is in header.php
file which means I cannot write unique class name for every page.You should refer to the current element and not all elements matching your selector.
$("#mainMenu td").click(function() {
$(this).css('background-color', '#EDEDED');
});
I´d also recommend you to use CSS classes instead of setting the CSS properties this way.
That would be something like;
$("#mainMenu td").click(function() {
$(this).addClass('selected');
});
together with;
#mainMenu td.selected {
background-color: #EDEDED; }
Try this (Do copy and paste):
Test.html:-
<html>
<link rel="stylesheet" type="text/css" href="style.css">
<a class="fruit" href="#">Home</a></span>
<a class="fruit" href="#">About</a></span>
<a class="fruit" href="#">Contact</a></span>
</html>
style.css:-
a:link{
color:blue;
}
a:visited{
color:purple;
}
a:hover{
color:orange;
}
a:focus{
color:green;
}
a:active{
color:red;
}
a:active{
color:yellow;
}
Another variation with a simple 2-line listener
$( ".menu_button" ).click(function() {
$( ".menu_button" ).removeClass('menu_button_highlight');
$(this).addClass('menu_button_highlight');
});
=====
<a class='menu_button' href='#admin'>Admin</a>
<br/>
<a class='menu_button' href='#user_manager'>User Manager</a>
<br/>
<a class='menu_button' href='#invite_codes'>Invite Codes</a>
====
.menu_button {
padding: 0 5px;
}
.menu_button_highlight {
background: #ffe94c;
}
Add a class to the body of each page:
<body class="home">
Or if you're on the contact page:
<body class="contact">
Then take this into consideration when you're creating your styles:
#sub-header ul li:hover,
body.home li.home,
body.contact li.contact { background-color: #000;}
#sub-header ul li:hover a,
body.home li.home a,
body.contact li.contact a { color: #fff; }
Lastly, apply class names to your list items:
<ul>
<li class="home"><a href="index.php">Home</a></li>
<li class="contact"><a href="contact.php">Contact Us</a></li>
<li class="about"><a href="about.php">About Us</a></li>
</ul>
This point, whenever you're on the body.home
page, your li.home a
link will have default styling indicating it is the current page.