【发布时间】:2019-08-22 21:59:39
【问题描述】:
试图在我的导航中制作“下一个”和“上一个”按钮。单击时,他们应该将我带到下一个/上一个 html 文件(网页)。
代码如下所示:
const pages =
["home.html","about.html","skills.html","projects.html","contact.html"];
const currentUrl = window.location.href;
const i = pages.indexOf(currentUrl)
const arrowUp = document.getElementById("up");
const arrowDown = document.getElementById("down");
function next(){
i++;
let goTo = pages[i];
window.location = goTo;
if(i===pages.length){
i = 0;
}
}
function prev(){
i--;
let goTo = pages[i];
window.location = goTo;
if(i=0){
i = pages.length;
}
}
arrowUp.addEventListener("click",next());
arrowDown.addEventListener("click",prev());
<div id="arrows">
<a href="#" id="up">
<i class="far fa-arrow-circle-up"></i>
</a>
<a href="#" id="down" class="mbot1">
<i class="far fa-arrow-circle-down"></i>
</a>
</div>
链接问题是因为href="#"吗?点击它只是在当前 url 上添加 # (就像没有 js 一样)。
【问题讨论】:
-
您对
i === pages.length的测试需要在let goTo = pages[i];之前进行。您对 应该i === 0而不是i = 0的测试存在同样的问题。
标签: javascript html