我试图在wordpress中给我的‘li’添加一个活动状态,我可以在dev工具中看到它正在添加这个类,但随后它就消失了。我是不是漏掉了什么明显的东西?这是Wordpress的导航。谢谢!
$(document).ready(function() {
$('#menu-nav a').click(function() {
$('#menu-nav a').removeClass('activeNav');
$(this).addClass('activeNav');
});
});
发布于 2014-06-26 17:38:51
正如Chris提到的,如果您点击的链接被重定向到另一个页面,导致整个页面重新加载,这并不奇怪。如果你想防止锚点重定向到另一个页面,你可以使用event.preventDefault()
:
$(document).ready(function() {
$('#menu-nav a').click(function(e) {
e.preventDefault(); // this will cancel the action of the link
$('#menu-nav a').removeClass('activeNav');
$(this).addClass('activeNav');
});
});
https://stackoverflow.com/questions/24436743
复制