如果条件不满足则条件不运行

时间:2018-02-24 02:24:52

标签: javascript html

仅在main.js语句中的else语句中读取score- = 1运行,if条件甚至在条件满足时也不会。即使点击了正确的选项,我的分数值也不会增加1而是总是减少1,这意味着它只满足else语句

的index.html

<div class="buttons">

<button id="button0"><span id="option0"></span></button>  
<button id="button1"><span id="option1"></span></button>  
<button id="button2"><span id="option2"></span></button>  
<button id="button3"><span id="option3"></span></button>  
 </div>

main.js

  var questions =[{

   question:'abcbcb',
  options:['a','b','c','d'],
  answer:'b' 
 },   { 
  question:"capital of india",
   options:['delhi','mum','pune','kol'],
   answer:'delhi'

 }]
 var x = Math.floor(Math.random() * (questions.length));
 var scores = 0;


 function gameplay(){
var quesn = document.getElementById('question');
quesn.innerHTML =questions[x].question;
for(i=0;i<4;i++){
var opt = document.getElementById('option'+i);
opt.innerHTML = questions[x].options[i];
var score = document.getElementById('scores');
score.innerHTML = scores;
}

}

 gameplay();

 for(i=0;i<4;i++){
     var y = document.getElementById('button'+i);
     var z = document.getElementById('option'+i);
     y.onclick = function(){
        if((z.innerHTML) ==(questions[x].answer)){
            scores +=1;
        }
        else{
            scores -=1;
        }
        x=Math.floor(Math.random() * (questions.length));
         gameplay();
     }
 }

3 个答案:

答案 0 :(得分:1)

对于纯Javascript,请使用innerHTML属性。

对于您的示例,请使用以下内容:

var spanVal = document.getElementById("option0").innerHTML;

答案 1 :(得分:0)

var x = document.getElementById(“option0”)。innerHTML;

的console.log(x)的

这就是你如何获得价值,“。innerText”也可以。

(顺便说一句,你把它标记为java中的问题,这是javascript。非常不同。

希望这有帮助。

答案 2 :(得分:0)

WORKING SAMPLE

替换此

 for(i=0;i<4;i++){
     var y = document.getElementById('button'+i);
     var z = document.getElementById('option'+i);
     y.onclick = function(){
        if((z.innerHTML) ==(questions[x].answer)){
            scores +=1;
        }
        else{
            scores -=1;
        }
        x=Math.floor(Math.random() * (questions.length));
         gameplay();
     }
   }

有了这个

 function answer(ans)
 {
var myAnswer = document.getElementById('option'+ans);
if(myAnswer.innerHTML == (questions[x].answer))
{
    scores += 1;
}
else{
    scores -= 1;
}
x=Math.floor(Math.random() * (questions.length));
     gameplay();

console.log(ans);
}

然后这个

 <p id="question"></p> 
  <div class="buttons">
 <button id="button0"><span id="option0"></span></button>  
 <button id="button1"><span id="option1"></span></button>  
 <button id="button2"><span id="option2"></span></button>  
 <button id="button3"><span id="option3"></span></button>  
 </div>
 <p id = 'scores'></p>

有了这个

    <p id="question"></p> 
<div class="buttons">
<button id="button0" onclick ="answer('0')"><span id="option0"></span></button>  
<button id="button1" onclick ="answer('1')"><span id="option1"></span></button>  
<button id="button2" onclick ="answer('2')"><span id="option2"></span></button>  
<button id="button3" onclick ="answer('3')"><span id="option3"></span></button>  
</div>
<p id = 'scores'></p>