【发布时间】:2016-04-17 18:59:39
【问题描述】:
我不知道如何根据函数中的条件迭代到下一个元素。一旦用户突出显示正确的单词并显示完成的消息,它应该转到从数组 onA 中获取的下一个定义/单词。在底部,我放了一个伪代码,因为我不知道如何从函数中访问条件语句的结果。
var onA = [
{t: "grooming", d: "an activity when someone builds an emotional connection with a child to gain their trust for the purposes of sexual abuse or exploitation."},
{t: "cyberbullying", d: "an activity that involves the use of ICT, particularly mobile phones and the internet, deliberately to upset, threaten and intimidate someone else."}
];
function getSelectionHandler(index) {
return function clickHandler() {
var txt = '';
var feedback = document.querySelector("#onA .feedback");
if (window.getSelection) {
txt = window.getSelection().toString();
} else if (document.selection) {
txt = document.selection.createRange().text;
}
// Display the selected text
document.querySelector("#onA .word").innerHTML = txt;
// Change the type of bootstrap alert depending on success
function feed(oldClass, newClass, message) {
feedback.classList.remove(oldClass);
feedback.classList.add(newClass);
feedback.innerHTML = message.bold();
}
// Check if the selected word is correct
if (txt === onA[index].t) {
feed("alert-warning", "alert-success", "Well done!");
} else {
feed("alert-success", "alert-warning", "Try again!");
}
};
}
var i = 0
while (i<onA.length) {
document.getElementById("onA").onclick = getSelectionHandler(i);
document.querySelector("#onA .def").innerHTML += onA[i].d;
if condition in the if statement above is true:
i++
}
【问题讨论】:
-
您是否希望只有在点击“美容”成功后才点击“网络欺凌”才能成功?还是没关系,两次点击是独立的?
-
首先显示'grooming'的定义,用户需要点击'grooming'。之后,将显示数组的第二项,即。网络欺凌的定义,然后他们将不得不点击“网络欺凌”。
标签: javascript function while-loop