-1
我們正在試圖將下面的數組「問題」外化。我們試圖將數組複製粘貼到一個名爲「qList.js」的外部JavaScript文件中,我們嘗試在標籤中包含如下內容: <script src ="qList.js">
。不幸的是,這是行不通的。有小費嗎?外部化Javascript數組
與包含在HTML數組的代碼如下(只是外部化前陣):
var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, chD, correct = 0;
var questions = [
["Question 1", "Choice1", "Choice2", "Choice3", "Choice4", "B"],
["Question 2", "Choice1", "Choice2", "Choice3", "Choice4", "C"],
["Quesiton 3", "Choice1", "Choice2", "Choice3", "Choice4", "A"]
];
function _(x) {
return document.getElementById(x);
}
function renderQuestion() {
test = _("test");
if (pos >= questions.length) {
test.innerHTML = "<h2>You got "+correct+" of "+questions.length+" questions correct</h2>";
_("test_status").innerHTML = "Test Completed";
pos = 0;
correct = 0;
return false;
}
_("test_status").innerHTML = "Question " + (pos+1) + " of " + questions.length;
question = questions[pos][0];
chA = questions[pos][1];
chB = questions[pos][2];
chC = questions[pos][3];
chD = questions[pos][4];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<input type='radio' name = 'choices' value='A'> "+chA+"<br>";
test.innerHTML += "<input type='radio' name = 'choices' value='B'> "+chB+"<br>";
test.innerHTML += "<input type='radio' name = 'choices' value='C'> "+chC+"<br>";
test.innerHTML += "<input type='radio' name = 'choices' value='D'> "+chD+"<br><br>";
test.innerHTML += "<button onclick='checkAnswer()'>Submit</button>";
}
function checkAnswer(){
choices = document.getElementsByName("choices");
for (var i=0; i<choices.length; i++) {
if (choices[i].checked) {
choice = choices[i].value;
}
}
if(choice == questions[pos][5]) {
correct++;
}
pos++;
renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
</script>
您還需要關閉''標籤。但是由於你沒有真正描述問題的原因,很難說這是否是唯一的問題(或者它是否是一個問題)。請詳細說明「這行不通」。到目前爲止,您已經做了什麼來調試該問題? –
你把那個標籤放在哪裏?請顯示完整的html標記,包括兩個腳本標記。 – Bergi
關閉''似乎仍然在代碼的底部。如果它實際上在文件中,則會中斷。看看你的開發者控制檯。 –