如何解决错误“ TypeError:无法读取null的属性'innerText'”

时间:2019-12-19 11:57:00

标签: javascript

我只是想调用一个函数而不能正常工作,而是将其隐藏错误:'TypeError:无法读取null的'innerText'属性',这就是我所拥有的

function countdown() { 
        setTimeout('Decrement()', 60); 
    } 
    //note
    //set minutes 
//        var mins = document.getElementById("min");




 var mins = parseInt(x);

    //calculate the seconds 
    var secs = mins * 60; 

    function getminutes() { 
        //minutes is seconds divided by 60, rounded down 
        mins = mins.value = Math.floor(secs / 60); 
        return mins; 
    } 

    function getseconds() { 
        //take minutes remaining (as seconds) away  
        //from total seconds remaining 
        return secs - Math.round(mins * 60); 
    } 



    //Decrement function decrement the value. 
    function Decrement() { 
        if (document.getElementById) { 
//                minutes = document.getElementById("min"); 
//                seconds = document.getElementById("sec"); 
//  
            //if less than a minute remaining 
            //Display only seconds value. 
            if (seconds < 59) { 
                seconds.value = secs; 
            } 

            //Display both minutes and seconds 
            //getminutes and getseconds is used to 
            //get minutes and seconds 
            else { 
                minutes.value = getminutes(); 
                seconds.value = getseconds(); 
            } 
            //when less than a minute remaining 
            //colour of the minutes and seconds 
            //changes to red 
            if (mins < 1) { 
                minutes.style.color = "white"; 
                minutes.style.backgroundColor = "#b70b0b"; 
                seconds.style.color = "white"; 
                seconds.style.backgroundColor = "#b70b0b"; 
            } 
            //if seconds becomes zero, 
            //then page alert time up 
            if (mins < 0) { 
                alert('YOUR TIME IS UP'); 
                minutes.value = 0; 
                seconds.value = 0; 

            } 
            //if seconds > 0 then seconds is decremented 
            else { 

                secs--; 
                setTimeout('Decrement()', 1000); 
            } 
        } 
        try{
            submit();
        }
        catch(e)
            {
                document.write(e);
            }

    } 

因此,正如您所看到的那样,只需将Submit函数放在try catch语句中以检查错误的出处,并向我显示错误,我什至尝试将代码提交到另一个函数中功能,但不起作用。

这是提交的文件

var ithing = 1;

var variable =1;
    var variable1 =1;
    var variable2 =1;
    var variable3 =1;
var slideIndex = 1;
    var slideIndex1 = 1;
    var score = 0;
    var score0 = 0;
    var score1 = 0;
    var score2 = 0;
    var score3 = 0;
    var y = 0;
showSlides(slideIndex);

showSlides1(slideIndex);
    showSlides2(slideIndex);
    showSlides3(slideIndex);
function submit()
{
    score = 0;
    score0 = 0;
    score1 = 0;
    score2 = 0;
    score3 = 0;
 var slides = document.getElementsByClassName("mySlides0");
    document.getElementById('result').innerHTML = '';
   for(var u = 0; u <=3; u++){
       document.getElementById('result').innerHTML += document.getElementById('sub'+u).innerText; 
       document.getElementById('result').innerHTML += '<br>';

  for (var i = 1; i <= 4; i++) {

      try{
     var checker = document.getElementById(i.toString() + u.toString()).value;
          }
       catch(e)
        {
            var chris = 0;
            continue;
        }

      var answer = document.getElementById('answer'+i.toString() + u.toString()).value;

      if(checker == answer && u == 0)
      {
          score0 += 1;
          score = score0;
        document.getElementById('result').innerHTML += 'QUESTION '+ i + " is CORRECT<br>";         
      }
      else if(checker == answer && u == 1)
      {
          score1 += 1;
          score = score1;
        document.getElementById('result').innerHTML += 'QUESTION '+ i + " is CORRECT<br>";         
      }
      else if(checker == answer && u == 2)
      {
          score2 += 1;
          score = score2;
        document.getElementById('result').innerHTML += 'QUESTION '+ i + " is CORRECT<br>";         
      }
      else if(checker == answer && u == 3)
      {
          score3 += 1;
          score = score3;
        document.getElementById('result').innerHTML += 'QUESTION '+ i + " is CORRECT<br>";         
      }
      else if(checker == '')
      {
        document.getElementById('result').innerHTML += 'QUESTION '+ i + " IS UNANSWERED THE ANSWER IS, " + answer + "<br>";
      }
      else{
        document.getElementById('result').innerHTML += checker + " IS WRONG!!! THE ANSWER IS " + answer + "<br>";
      }

    }
       document.getElementById('result').innerHTML +=  'YOUR SCORE IS' + score + '/' + document.getElementsByClassName("mySlides"+u).length + '<br><br>';
       score = 0;
       y  += u;
       var g = y+1;
   }
    var best = score0 + score1 + score2 + score3;
    document.getElementById('result').innerHTML +=  '<p style="color:#5454de;">YOUR OVERALL SCORE IS ' + best + '/' + g+ '</p><br>';
}

我需要帮助,谢谢!

0 个答案:

没有答案