【问题标题】:Javascript - iterate to the next element based on conditionJavascript - 根据条件迭代到下一个元素
【发布时间】: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


【解决方案1】:

全局变量可用于记录当前状态。我只是亲手写的,如果有需要你必须调试它。

function getSelectionHandler(){
    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!");
        index++;    //increase before showing next or checking end
        if (checkEnded()){
            //all end, show end msg
            feed("alert-warning", "alert-success", "Finish!");
        }else {
            //show next
            showNext();
        }
    } else {
        feed("alert-success", "alert-warning", "Try again!");
    }
}

function checkEnded(){
    return index >= onA.length;
}

function showNext(){
    document.querySelector("#onA .def").innerHTML += onA[index].d;  //display next item

}

var index = 0;  //global index for selecting from the array
document.getElementById("onA").onclick = function(){getSelectionHandler();} //register hanlder onc time only
document.querySelector("#onA .def").innerHTML += onA[index].d;      //display first item

【讨论】:

  • 谢谢,但是高亮机制和反馈功能已经停止工作。更糟糕的是,它不会在控制台中引发任何错误。一直试图找出原因。
  • 好的。得到它的工作。只需要从最后一行中删除 () 即可。现在唯一剩下的就是在 index++ 之前放一些东西来确认用户想要继续下一个问题。
  • 很高兴看到你让它工作。我只是查看w3schools.com/jsref/event_onclick.asp。似乎 onclick 句柄的语句应该是 document.getElementById("onA").onclick = function(){getSelectionHandler();}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 2015-04-29
  • 1970-01-01
  • 2016-03-29
相关资源
最近更新 更多