首页 > 解决方案 > 一种将参数添加到变量或其他东西的方法,这样我就不会向函数和变量发送垃圾邮件

问题描述

所以,像游戏这样的网站 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");
    }
}

标签: javascriptparametersparameter-passing

解决方案


推荐阅读