我有<li>
标签中嵌入的链接列表,例如
<ul>
<li><a href='/111'>Link1</a></li>
<li><a href='/555'>Link1</a></li>
<li><a href='/444'>Link1</a></li>
<li><a href='/222'>Link1</a></li>
<li><a href='/333'>Link1</a></li>
</ul>
我已经写了一个javascript函数,它执行以下操作,当前加载的页面的window.location.pathname并且取决于传递的param更改位置以指向下一个链接或上一个链接,当我从开发人员运行该函数时控制台它工作正常,但当我通过正常路线触发有问题的功能,即通过点击下一个/上一个链接它,我无法确定我错过了什么。
功能代码: -
window.navigate_to = function(event, direction) {
var current_location, next_url, node, prev_url, url;
if (event.preventDefault) {
event.preventDefault();
} else if (event.stop) {
event.stop();
} else {
"";
}
if (direction == null) {
direction = 1;
}
current_location = window.location.pathname;
console.log(current_location);
node = document.querySelector("a[href*='" + current_location + "']");
if (node != null) {
next_url = node.parentNode.nextElementSibling != null ? node.parentNode.nextElementSibling.querySelector("a[href]").getAttribute('href') : "";
prev_url = node.parentNode.previousElementSibling != null ? node.parentNode.previousElementSibling.querySelector("a[href]").getAttribute('href') : "";
console.log(node);
console.log(next_url, prev_url);
url = direction === 1 ? next_url : prev_url;
console.log(window);
console.log(url);
window.location = window.location.host + url;
return true;
}
};
TIA