2014-09-30 101 views
0

什麼是最好的方法來刪除下面的當前問題值,並添加下一個問題,點擊按鈕時。做了一個糟糕的嘗試,並可以做一些幫助。繼承人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; 
    } 
} 

回答

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

我不確定這是不是你想要的,但這至少會改變每次點擊的問題。

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

剛注意到第一個答案和這個差不多,這個答案循環回到最後一個問題之後的第一個問題。 – mikeLspohn 2014-10-01 00:39:40

+0

也可能不應該使用我作爲變量,除非您想將整個答案封裝在一個iife中以將其全部封裝到該範圍中。 – mikeLspohn 2014-10-01 00:44:36