Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 测验-添加和删除节点_Javascript - Fatal编程技术网

Javascript 测验-添加和删除节点

Javascript 测验-添加和删除节点,javascript,Javascript,单击按钮时,删除下面的当前问题值并添加下一个问题的最佳方法是什么。做了一次糟糕的尝试,需要一些帮助。这是菜单 var-allQuestions=[ { 问题:“谁是联合王国首相?”, 选择:[“大卫·卡梅伦”、“戈登·布朗”、“温斯顿·丘吉尔”、“托尼·布莱尔”], 正确答案:0 },{ 问题:“什么是4+4?”, 选择:[“24”、“8”、“18”、“16”], 正确答案:0 },{ 问题:“什么是5+4?”, 选择:[“24”、“9”、“10”、“16”], 正确答案:0 }]; var b

单击按钮时,删除下面的当前问题值并添加下一个问题的最佳方法是什么。做了一次糟糕的尝试,需要一些帮助。这是菜单

var-allQuestions=[
{
问题:“谁是联合王国首相?”,
选择:[“大卫·卡梅伦”、“戈登·布朗”、“温斯顿·丘吉尔”、“托尼·布莱尔”],
正确答案:0
},{
问题:“什么是4+4?”,
选择:[“24”、“8”、“18”、“16”],
正确答案:0
},{
问题:“什么是5+4?”,
选择:[“24”、“9”、“10”、“16”],
正确答案:0
}];
var button=document.getElementById('next');
var question=document.getElementById(“问题”);
button.onclick=函数(){

对于(var i=0;i,这里是更新的代码。请查看

下面的代码按顺序显示

下面的代码显示随机顺序

--随机的

 var allQuestions = [
{
    question: "Who is Prime Minister of the United Kingdom?", 
    choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"], 
    correctAnswer:0
},{
    question: "What is 4 + 4?", 
    choices: ["24", "8", "18", "16"], 
    correctAnswer:0
},{
    question: "What is 5 + 4?", 
    choices: ["24", "9", "10", "16"], 
    correctAnswer:0
}];

var button = document.getElementById('next');
var question = document.getElementById("question");
button.onclick = function(){    
for(var i=0; i<allQuestions.length;i++){
    var rnd = Math.floor(Math.random() * allQuestions.length);
  question.innerHTML = allQuestions[rnd].question;
}
}

我不确定这是否是你想要的,但这至少改变了每次点击的问题

var i = 0;
button.onclick = function(){
    question.innerHTML = allQuestions[i].question;
      i++;
    if(i === 3){
      i = 0;
    }
};

注意到第一个答案和这个差不多,减去这个答案,循环回到最后一个问题之后的第一个问题。也可能不应该使用i作为变量,除非你想把整个答案封装在一个iLife中,将其封装到这个范围内。
 var allQuestions = [
{
    question: "Who is Prime Minister of the United Kingdom?", 
    choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"], 
    correctAnswer:0
},{
    question: "What is 4 + 4?", 
    choices: ["24", "8", "18", "16"], 
    correctAnswer:0
},{
    question: "What is 5 + 4?", 
    choices: ["24", "9", "10", "16"], 
    correctAnswer:0
}];

var button = document.getElementById('next');
var question = document.getElementById("question");
button.onclick = function(){    
for(var i=0; i<allQuestions.length;i++){
    var rnd = Math.floor(Math.random() * allQuestions.length);
  question.innerHTML = allQuestions[rnd].question;
}
}
var rnd = Math.floor(Math.random() * allQuestions.length);
var i = 0;
button.onclick = function(){
    question.innerHTML = allQuestions[i].question;
      i++;
    if(i === 3){
      i = 0;
    }
};