为了账号安全,请及时绑定邮箱和手机立即绑定

我正在制作抽认卡游戏,但“下一步”按钮不起作用

我正在制作抽认卡游戏,但“下一步”按钮不起作用

繁星点点滴滴 2021-10-07 10:39:29
我正在制作“下一步”按钮以移至此游戏中的下一张抽认卡,但是当我单击该按钮时,什么也没有发生。我不知道我做错了什么。我在这段代码中使用了很多变量,例如数字变量来说明它是哪个问题和答案。我尝试每次向 number 变量添加一个,以将其更改为下一个问题,但它不起作用。    <div id="flashcard" onclick="flip()"><h1 id="word"></h1></div><br><button onlick="next()">Next</button><script>var questions;questions = ['artistic','daring','good','sports-minded','messy','disorganized','studious','funny','impacient','intelligent','neat','patient','lazy','shy','serious','nice','sociable','talented','hardworking','boy','girl','male friend','female friend','I','he','she','very','according to my family'];var answers = ['artístico, artística','atrevido','bueno, buena','deportista','desordenado, desordenada','estudioso, estudiosa','gracioso, graciosa','impaciente','intelligente','ordenado, ordenada','paciente','perezoso, perezosa','reservado, reservada','serio, seria','simpático, simpática','sociable','talentoso, talentosa','trabajador, trabajadora','el chico','la chica','el amigo','la amiga','yo','él','ella','muy','según mi familia'];var number = 0;var answer = answers[number];var word = questions[number];var display = document.getElementById('word');display.innerHTML = word;function flip(){if(word == questions[0]){display.innerHTML = answer;}if (word == questions[1]){display.innerHTML = answer;}if (word == questions[2]){display.innerHTML = answer;}if (word == questions[3]){display.innerHTML = answer;}if (word == questions[4]){display.innerHTML = answer;}if (word == questions[5]){display.innerHTML = answer;}if (word == questions[6]){display.innerHTML = answer;}if (word == questions[7]){display.innerHTML = answer;}if (word == questions[8]){display.innerHTML = answer;}if (word == questions[9]){display.innerHTML = answer;}if (word == questions[10]){display.innerHTML = answer;}if (word == questions[11]){display.innerHTML = answer;}if (word == questions[12]){display.innerHTML = answer;}if (word == questions[13]){display.innerHTML = answer;}我想要发生的是当用户单击“下一步”按钮时,它会更改为下一个闪存卡。
查看完整描述

2 回答

?
茅侃侃

TA贡献1842条经验 获得超21个赞

如果使用变量来访问数组中的数据,则可以大大简化代码 - 您不必对代码中的每个索引进行硬编码。我想你希望你的代码像这样工作:


var questions;

questions = ['artistic', 'daring', 'good', 'sports-minded', 'messy', 'disorganized', 'studious', 'funny', 'impacient', 'intelligent', 'neat', 'patient', 'lazy', 'shy', 'serious', 'nice', 'sociable', 'talented', 'hardworking', 'boy', 'girl', 'male friend', 'female friend', 'I', 'he', 'she', 'very', 'according to my family'];

var answers = ['artístico, artística', 'atrevido', 'bueno, buena', 'deportista', 'desordenado, desordenada', 'estudioso, estudiosa', 'gracioso, graciosa', 'impaciente', 'intelligente', 'ordenado, ordenada', 'paciente', 'perezoso, perezosa', 'reservado, reservada', 'serio, seria', 'simpático, simpática', 'sociable', 'talentoso, talentosa', 'trabajador, trabajadora', 'el chico', 'la chica', 'el amigo', 'la amiga', 'yo', 'él', 'ella', 'muy', 'según mi familia'];


//Your code doesnt have to check every single index, just use a variable

var display = document.getElementById('word');

var next = document.getElementById('next');

let i = 0;


function flip(idx) {

  display.innerHTML = answers[idx];


  if (i < answers.length - 1) {

    //Increment the counter by 1 on each flip

    i += 1;

  } else {

    //Back to first card

    i = 0;

  }


}


//Flip the first card right away

flip(0);


//Add an event handler to the button to call flip when the button is clicked

next.addEventListener('click', () => {

  flip(i);

});

<input type="button" id="next" value="Next">

<div id="word"></div>


查看完整回答
反对 回复 2021-10-07
  • 2 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信