Javascript无法在应用程序上运行
Javascript not working on app
所以我正试图根据这篇博客文章中的代码构建一个智力竞赛应用程序(codepen示例)。
我已经做了所有的事情,但JS似乎不起作用,因为除了HTML和CSS之外,没有任何问题或答案或任何东西出现。这是我的JSFiddle尝试和代码
window.onload = function () {
var questionArea = document.getElementsByClassName('questions')[0],
answerArea = document.getElementsByClassName('answers')[0],
checker = document.getElementsByClassName('checker')[0],
current = 0,
allQuestions = {
//question and answer list, the number is the index of the answers
'madrugada' : ['journey', 'dawn', 'mother', 1],
'manzana' : ['apple', 'insane', 'men', 0],
'vivir' : ['villain', 'to live', 'to go', 1],
'amarillo' : ['love', 'river', 'yellow', 2],
'almendra' : ['almond', 'cheese', 'rails', 0],
'cascabel' : ['jingle bell', 'helmet', 'beauty', 0],
'aceituna' : ['tuna', 'oil', 'olive', 2],
'azar' : ['king', 'chance', 'zebra', 1],
'milagro' : ['voyage', 'tea', 'miracle', 2],
'añoranza' : ['snore', 'dusk', 'longing', 2],
'abecedario' : ['cedar', 'alphabet', 'ability', 1],
'frenesí' : ['frenzy', 'freaky', 'neat', 0],
'hechizo' : ['spell', 'done', 'zone', 0],
'alma' : ['almond', 'soul', 'leaf', 1],
'mariposa' : ['marriage', 'pose', 'butterfly', 2],
'siempre' : ['person', 'always', 'simple', 1],
'anochecer' : ['dusk', 'anual', 'dawn', 0],
'chiste' : ['clean', 'cheese', 'joke', 2],
'ojo' : ['eye', 'eight', 'dot', 0],
'ojalá' : ['eyeball', 'hopefully', 'lullaby', 1]
};
function loadQuestion(curr) {
//Loads questions into question area
var question = Object.keys(allQuestions)[cur];
//remove everything in q area and add current question in
questionArea.innerHTML = '';
questionArea.innerHTML = question;
}
function loadAnswers(curr) {
//Loads all the possible answers of the given question
var answers = allQuestions[Object.keys(allQuestions)[curr]];
//empty answer area
answerArea.innerHTML = '';
//add possible answers to answerArea
for (var i = 0; i < answers.length - 1; i++) {
var createDiv = document.createElement('div'),
text = document.createTextNode(answers[i]);
createDiv.appendChild(text);
//adds an onclick function to the answer; a click will execute a function to check if the answer was correct
createDiv.addEventListener("click", checkAnswer(i, answers));
answerArea.appendChild(createDiv);
}
}
function checkAnswer(i, arr) {
//checks if answer given is same as the correct one, and if it is the last question. If it is the last question, it empties answerArea
return function() {
var givenAnswer = i,
correctAnswer = arr[arr.length - 1];
if (givenAnswer === correctAnswer) {
addChecker(true);
} else {
addChecker(false);
}
if (current < Object.keys(allQuestions).length - 1) {
current++;
loadQuestion(current);
loadAnswers(current);
} else {
questionArea.innerHTML = '¡Fin!';
answerArea.innerHTML = '';
}
};
}
function addChecker(bool) {
//adds div element to page, used to see whether answer was correct or false
var createDiv = document.createElement('div'),
txt = document.createTextNode(current + 1);
createDiv.appendChild(txt);
if (bool) {
createDiv.className += 'correct';
checker.appendChild(createDiv);
} else {
createDiv.className += 'false';
checker.appendChild(createDiv);
}
}
};
谢谢你的帮助!
您没有调用启动和运行所需的函数,而是只在代码中定义了它们。就这样称呼他们吧。。。
// Start the quiz right away
loadQuestion(current);
loadAnswers(current);
另外,不要为JSFiddle的window.onload
而烦恼。
JSFiddle链接-更新的示例
相关文章:
- 使用Node.js在应用程序引擎上进行本地单元测试
- 谷歌应用程序脚本上有承诺吗
- 在全球范围内强制ng严格di,而不仅仅是在ng应用程序级别上
- 如何加载/构建 Chrome 应用/扩展程序并以编程方式运行
- Krajee 引导程序文件上传插件仅接受图像文件类型
- 在 ipad 应用程序网页上触发 HREF
- 从谷歌应用程序脚本上的json发布到Facebook
- 我可以用谷歌应用程序脚本上传多个文件吗
- 在应用程序代码上运行Google Closure编译器后,更新AngularJS模板HTML
- 如何将变量从python应用程序传递到google应用程序引擎上的客户端javascript
- 使用表单输入访问相机,并使用web应用程序立即上传照片
- 如何在我的移动应用程序(cordova)上启用跨域资源共享,它连接到API(在Yii中)
- 页面过渡像iPhone应用程序feedly上的phonegap
- 当用户在应用程序图标上键入时,如何触发一些代码
- 在web应用程序相机上覆盖透明png
- 我如何从谷歌应用程序脚本上的CalendarEvent对象获得颜色
- 如何禁用Web应用程序浏览器上的关闭按钮
- 谷歌应用程序脚本上的代码安全问题
- <脚本>标签在桌面浏览器上有效,但在移动设备(MEAN 应用程序)上不起作用
- 混合应用程序在上传图像时关闭而不会在logcat中出现任何错误