就像@Luke所说,您没有添加任何链接供面包屑导航。您必须像这样存储文本和链接:
function breadcrumbStateSaver(link, text) {
if (typeof (Storage) != "undefined") {
if (sessionStorage.breadcrumb) {
sessionStorage.breadcrumb = sessionStorage.breadcrumb + ";" + text;
sessionStorage.locations = sessionStorage.locations + ";" + link; //use any delim you prefer
}
else {
sessionStorage.breadcrumb = text;
sessionStorage.locations = link;
}
}
您可以将文本和链接都添加到一个存储变量中,然后用分隔符将其拆分以提取它!
而不是在点击时捕获链接,您实际上可以在页面加载时获取链接document.location.href
,并且文本可以取自document.title
或将在 server.xml 中设置的某些属性。所以在 document.ready
$(document).ready(function(){
breadcrumbStateSaver(document.title, document.location.href);
showBreadCrumb();
});
要更新 a 中的面包屑<ul>
:
function showBreadCrumb(){
let crumbs = sessionStorage.breadcrumb.split(";");
let links = sessionStorage.locations.split(";");
for(let index in crumbs)
$("#navigation_links").append($("<li><a href="+links[index]+">"+ crumbs[index] +"</a></li>"));
}
这样,每次用户浏览您的网站时,它都会记录下来。如果您希望用户通过面包屑导航,通过单击面包屑返回他们所在的页面,将上面的代码更改为以下内容:
function showBreadCrumb(){
let crumbs = sessionStorage.breadcrumb.split(";");
let links = sessionStorage.locations.split(";");
let crumbindex = parseInt(sessionStorage.crumbindex);
for(let index in crumbs){
$("#navigation_links").append($("<li><a class='crumb"+ (index==crumbindex ? "active" : "") + "' href='"+links[index]+"' data-node='"+index+"'>"+ crumbs[index] +"</a></li>")); //data-node will help in retrieving the index and class will be set to active for selected node
}
}
还添加:
$(document).ready(function(){
$('#navigation_links').on('click', 'li a.crumb', function(){
if (typeof (Storage) != "undefined") {
sessionStorage.crumbindex = $(this).data('node');
sessionStorage.navlinkclicked = "true"; //This will prevent from adding breadcrumb again
}
});
breadcrumbStateSaver(document.title, document.location.href);
showBreadCrumb();
});
使用节点索引,您可以像这样插入面包屑:
function breadcrumbStateSaver(link, text) {
if (typeof (Storage) != "undefined") {
if (sessionStorage.breadcrumb) {
if (sessionStorage.navlinkclicked && sessionStorage.navlinkclicked=="true") {
sessionStorage.navlinkclicked = "false"; //prevent changes or you can remove the rest of the crumbs from sessionStorage.breadcrumb and locations using a for loop
}
else {
if(sessionStorage.crumbindex && sessionStorage.crumbindex!=-1) {
let crumbs = sessionStorage.breadcrumb.split(";");
let links = sessionStorage.locations.split(";");
let crumbindex = parseInt(sessionStorage.crumbindex);
sessionStorage.breadcrumb = crumbs[0];
sessionStorage.locations= links[0];
for(let index in crumbs){
if(index==0) continue;
sessionStorage.breadcrumb = sessionStorage.breadcrumb + ";" + crumbs[index];
sessionStorage.locations= sessionStorage.locations + ";" + links[index];
if(index==crumbindex) break;
}
sessionStorage.breadcrumb = sessionStorage.breadcrumb + ";" + text;
sessionStorage.locations = sessionStorage.locations + ";" + link; //insert the new link after crumbindex location
sessionStorage.crumbindex = -1;
}
else{
sessionStorage.breadcrumb = sessionStorage.breadcrumb + ";" + text;
sessionStorage.locations = sessionStorage.locations + ";" + link; //keep appending
}
}
}
else {
sessionStorage.breadcrumb = text;
sessionStorage.locations = link;
}
}
如果您只是想要导航的面包屑,而不是记录用户的遍历,您可以简单地使用window.location.pathname
. 鉴于您的页面存储在具有索引页面的适当层次结构中,您所要做的就是拆分 url 路径并相应地更新 showBreadCrumb() 。您不需要使用会话存储!
function showBreadCrumb(){
let crumbs = window.location.pathname.split("/");
for(let index=0; index<crumbs.length-1; index++){
let text = crumbs[index];
if(index==0) text = "Home";
$("#navigation_links").append($("<li><a class='crumb"+ (index==crumbs.length-2? "active" : "") +"' href='"+buildRelativeLink(crumbs.length-index)+"'>"+ text +"</a></li>"));
}
}
function buildRelativeLink(level)
{
level = level - 1;
let link="";
for (let i=1; i<level; i++)
{
link=link+ "../"; //Used to navigate one level up towards parent
}
return link;
}