测验 - 添加和删除节点

时间:2014-09-30 23:26:35

标签: javascript

删除下面的当前问题值的最佳方法是什么,并在单击按钮时添加下一个问题。做了一个糟糕的尝试,可以做一些帮助。继承人jsfiddle.

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++){
    question.innerHTML = allQuestions[i].question;
  }
}

2 个答案:

答案 0 :(得分:0)

这是更新的代码。看看吧。

以下代码按顺序显示

http://jsfiddle.net/seshakiran/9675fvu2/17/

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");
var cnt=0;
button.onclick = function(){

  question.innerHTML = allQuestions[cnt].question;
cnt+=1;

}

以下代码显示随机顺序

http://jsfiddle.net/seshakiran/9675fvu2/11/ - 随机

 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);

答案 1 :(得分:0)

我不确定这是否适合您,但这至少会改变每次点击的问题。

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