我在js中编写了一个脚本,根据url页面在我的页面中添加了一个类,并且它可以正常工作。但是,在该页面中提交表单后,活动类将消失。我注意到URL也发生了变化。出现的页面不是表单所使用的servlet的URL映射。这附近有工作吗?也就是说,是否可以让班级保持活跃状态?或者我是否必须根据URL映射对其他脚本进行硬编码?
$(document).ready(function() {
addActiveClassToMenu();
});
function addActiveClassToMenu() {
// Get the current URL path
var URLpath = window.location.pathname;
// Store the page name in a variable
URLpage = URLpath.split("/").pop();
// Iterate through the DOM elements
$("nav ul li a").each(function() {
// Get the href attribute of the current iterated element
var hrefPath = $(this).attr('href');
// Get the page name
var hrefPage = hrefPath.split("/").pop();
// compare
if(hrefPage == URLpage) {
$(this).parent().addClass("active");
}
});
}
答案 0 :(得分:0)
在表单发布之前和表单提交之后将旧的url存储在cookie中,当你回到同一页面然后检查是否(hrefPage == $ .cookie(" oldUrl")){//添加活动类再来一次}