我正在做一个简短的问答游戏,我似乎无法弄清楚哪种事件侦听器适合检测文本框中的特定字符串。这是我到目前为止的内容(仅当“ else if”部分被取出而未检测到特定字符串时才有效)

HTML:

    <div id="ques1" class="ques">
     <img src="img/img1.png" alt="">

     <div id="btncontainer" class="btncontainer">
      <input id="txtbox" placeholder="Type here"/>
      <button id="go1" class="gobttn"><i>NEXT<i></button>
     </div>
    </div>

    <div id="ques2" class="ques">
     <img src="img/img2.png" alt="">
    </div>


JS:

var s2 = document.getElementById('ques1')
var s3 = document.getElementById('ques2')

var b1 = document.getElementById('landbtn')
var b2 = document.getElementById('go1')

var tbox = document.getElementById('txtbox')

function Quest1To2() {
 s2.style.display = 'none';
 s3.style.display = 'block';
}
b2.addEventListener('click', Quest1To2)

function TxtErr() {
 tbox.style.outline = '0.2em solid red';
}
b2.addEventListener('click', TxtErr)


var a1 = "NEO";
var answer = txtbox.value

if (answer == a1) {
  Quest1To2();
}

else if () {
  TxtErr();
}

最佳答案

当在input处发生输入时,可以使用tbox事件调度事件。

tbox.oninput = function(e) {
  if (e.target.value == a1) {
    Quest1To2();
  }
  else {
    TxtErr();
  }
}

08-19 16:35