如果菜单中的链接属于当前 URL,我需要更改该链接的外观。我不知道如何检测当前 URL 并更改元素类。
我会使用 jQuery
最佳答案
$( document).ready(function (){
$( function(){
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace( /\/$/,'' ) + "$" );
$('a').each( function(){
if(urlRegExp.test(this .href.replace(/\/$/, ''))){
$( this).addClass('active' );
}
});
});
});
关于jquery - 在事件菜单链接上添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14302911/