javascript - 一种将参数添加到变量或其他东西的方法,这样我就不会向函数和变量发送垃圾邮件
问题描述
所以,像游戏这样的网站 JavaScript 的重点是创建触发器。Const pgUnlock 是开关。我想要它,以便当查看者单击按钮时,我将更改元素以将 false 变为 true。例如,在下面,它检查数字 1 是否在 [0] 索引中,如果是,它将解锁网站的新部分,方法是将 # 替换为下一页链接。'alertbox()' 函数会检查这一点,如果触发器没有打开,它会显示一个按钮,让玩家知道“页面已锁定”。所以我的问题是如何添加参数来整合代码,这样我就不会再创建 5 个“alertbox()”函数,因为必须为“pgUnlock”的每个位置自定义“检查”变量。如果没有,我可以重复代码,我只是想看看我/我们是否可以通过使用更简洁的代码来保持一切性能友好,因为我对 JavaScript 不太熟悉。为了让你知道我有多愚蠢,我的尝试之一是:'var check(x,y)= pgUnlock.includes(x,y);'
var a = document.querySelector('.rebtn[href="#"]');
const pgUnlock= [0,0,0,0,0];
var check= pgUnlock.includes(1,0);
var check2= pgUnlock.includes(2,0);
var check3= pgUnlock.includes(3,0);
var check4= pgUnlock.includes(4,0);
var check5= pgUnlock.includes(5,0);
if (check==true) {
a.setAttribute('href', './introPage.html');
}
reBtn.onclick=()=>{
alertbox();
}
legBtn.onclick=()=>{
alertbox();
}
godBtn.onclick=()=>{
alertbox();
}
chgBtn.onclick=()=>{
alertbox();
}
ch2Btn.onclick=()=>{
alertbox();
}
function alertbox(){
if(check==false){
alertBtn.classList.add("visalert");
}
}
解决方案
推荐阅读
- html - 自动完成“打开”在 ReactJs 表单中不起作用
- chef-infra - 食谱中的厨师错误。缺少带刀的食谱
- google-api - Google Calendar API 获取上一页
- php - 如何在 laravel 刀片中拆分 foreach 循环
- django - 在 django 中使用 OneToOne Realtion 将用户信息管理到两个表中的最佳方法
- openrefine - 如何在 OpenRefine 中就地编辑列?
- python - 在 Python 中搜索和过滤元组列表
- angular - Angular 5 ngOnInit 多次调用
- c# - ASP.NET Core 2 - 切换活动目录用户
- python - 可以发送字符串,但没有别的