简单的面包屑

时间:2017-04-17 11:20:05

标签: javascript breadcrumbs

我有一个我发现的面包屑代码,但它显示了整个链接。如何制作一个类似的简单面包屑,只显示基于路径的页面名称? (仅限JavaScript) (也可以改变背景颜色)

<SCRIPT LANGUAGE="JavaScript">


var path = "";
var href = document.location.href;
var s = href.split("/");
for (var i=2;i<(s.length-1);i++) {
path+="<A HREF=\""+href.substring(0,href.indexOf("/"+s[i])+s[i].length+1)+"/\">"+s[i]+"</A> / ";
}
i=s.length-1;
path+="<A HREF=\""+href.substring(0,href.indexOf(s[i])+s[i].length)+"\">"+s[i]+"</A>";
var url = window.location.protocol + "//" + path;
document.writeln(url);

</script>

1 个答案:

答案 0 :(得分:0)

你的意思是它不会包含在面包屑中的网站吗?

<script language="Text/JavaScript">


var path = "";
var href = document.location.href;
var s = href.split("/");
for (var i=2;i<(s.length-1);i++) {
  path+="<A HREF=\""+href.substring(0,href.indexOf("/"+s[i])+s[i].length+1)+"/\">"+(i == 2 ? "home" : s[i])+"</A> / ";
}
i = s.length-1;
path += "<A HREF=\""+href.substring(0,href.indexOf(s[i])+s[i].length)+"\">"+s[i]+"</A>";
var url = path;
document.writeln(url);

</script>