Every page on my site has multiple body classes that I use CSS selectors with, like so: <bod开发者_如何学运维y class="category subcategory page">
I would like to be able to automatically add the class 'expanded' to the menu item in the left nav if it matches the final class in that declaration.
So far, my understanding of it is that I can set a variable to the body's last class name using className().last(); and then just write and if statement to determine if the menu item (an unordered list item) matches it and addClass('expanded');
But it's not working. Would someone be kind enough to help me with the syntax? I'm still a bit new to JavaScript.
$(document).ready(function () {
var pageclass=('body').className().last();
if ($('div.nav-menu li').hasClass(pageclass)){
$(this).addClass('current');
}
});
Not sure if className()
is a real method.. Try this:
var last_class = $('body').attr('class').split(' ').slice(-1);
if ($('div.nav-menu li.arrowed').hasClass(last_class)){
..
}
精彩评论