这不起作用的原因是因为javascript正在执行,然后页面正在重新加载,这使“活动”类无效。您可能想做的是:
$(function(){ var current = location.pathname; $('#nav li a').each(function(){ var $this = $(this); // if the current path is like this link, make it active if($this.attr('href').indexOf(current) !== -1){ $this.addClass('active'); } })})在某些情况下,这是行不通的(多个类似指向的链接),但是我认为这可能对您有用。



