Javascript 实施多层次深度链接
我需要为我的站点实现deeplinking,因为它大量使用AJAX构建。我理解基本的想法,并且已经为我的顶级导航实现了深度链接。问题是我的网站有多个级别。用户可以单击顶部导航,这将添加到哈希中,然后单击辅助导航和三级导航。我还需要将这个二级和三级nav点击添加到散列中…然后当用户点击主nav项目时,也能够删除该项目。我想不出一个办法…有什么想法吗 哦,我用来在主nav上实现哈希的代码(jQuery)是:Javascript 实施多层次深度链接,javascript,jquery,Javascript,Jquery,我需要为我的站点实现deeplinking,因为它大量使用AJAX构建。我理解基本的想法,并且已经为我的顶级导航实现了深度链接。问题是我的网站有多个级别。用户可以单击顶部导航,这将添加到哈希中,然后单击辅助导航和三级导航。我还需要将这个二级和三级nav点击添加到散列中…然后当用户点击主nav项目时,也能够删除该项目。我想不出一个办法…有什么想法吗 哦,我用来在主nav上实现哈希的代码(jQuery)是: updateHash : function(hash) { var hashHist
updateHash : function(hash) {
var hashHistory = [];
location.hash = hash;
}
我不太清楚你在找什么。也许你应该举一些具体的例子。在那之前,像这样的事情怎么样:
function updateHash(hashPath) {
if (hashPath.charAt(0) == "/") {
location.hash = "#" + hashPath;
return;
}
var currentHash = location.hash.split(/\//);
if (currentHash[0]) currentHash[0] = currentHash[0].substr(1); // Loose the #
var relHash = hashPath.split(/\//);
var part;
while (part = relHash.shift()) {
if (part == "..") {
currentHash.pop();
} else {
currentHash.push(part);
}
}
if (currentHash.length > 0)
location.hash = "#" + currentHash.join("/");
else
location.hash = "";
}
示例:
updateHash("/topLevelItem");
alert(location.href); // "www.example.com/#/topLevelItem"
updateHash("secondLevelItem");
alert(location.href); // "www.example.com/#/topLevelItem/secondLevelItem"
updateHash("thirdLevelItem");
alert(location.href); // "www.example.com/#/topLevelItem/secondLevelItem/thirdLevelItem"
updateHash("../differentThirdLevelItem");
alert(location.href); // "www.example.com/#/topLevelItem/secondLevelItem/differentThirdLevelItem"
updateHash("../../differentSecondLevelItem");
alert(location.href); // "www.example.com/#/topLevelItem/differentSecondLevelItem"
updateHash("../anotherSecondLevelItem/anotherThirdLevelItem");
alert(location.href); // "www.example.com/#/topLevelItem/anotherSecondLevelItem/anotherThirdLevelItem"
updateHash("..");
alert(location.href); // "www.example.com/#/topLevelItem/anotherSecondLevelItem"
任何人问题还不清楚吗?那代码看起来很有前途,谢谢!不过我还是有点困惑。如果用户单击了第二级项目,而我使用了updateHash函数添加哈希,那么我如何知道如果用户单击了下一轮需要发送的另一个第二级链接../differentsecondlevellink而不是updateHash函数的/secondlevellink?我是否要跟踪用户处于哪个级别,然后执行一些逻辑,比如如果用户处于第二级别,并且他们正在单击第二级别链接,然后在参数发送到updateHash之前追加../呢?没关系…我现在已设法使其全部工作。非常感谢你的帖子…它真的帮助了我!