javascript - 尝试制作循环通过导航 onclick 的“下一个”和“上一个”按钮
问题描述
试图在我的导航中制作“下一个”和“上一个”按钮。单击时,他们应该将我带到下一个/上一个 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
用const
. 那就不能重新分配了。使用var
或let
var i = pages.indexOf(currentUrl)
推荐阅读
- android - 用 Mac 的键盘控制 Android 应用程序模拟器?
- python - 得到
而不是城市名称 - typescript - 从返回多个(异构)值的 TypeScript 方法中获取值
- python - 使用 pyyaml 转储嵌套列表失败
- php - 如何保护 php 中的 cookie?
- python - 存储在 .git 文件夹中的存储库信息(如 url 、工作分支)在哪里?
- sql - 在 PostgreSQL 中实现嵌套聚合的正确方法
- r - 基于参数的长组合调度 S4 方法
- reactjs - 处理 children.props
- android - Google Play 内部测试版轨道在 14 小时后未显示更新