簡體   English   中英

嘗試更改innerHTML時出錯-無法將屬性'innerHTML'設置為null

[英]Error trying to change innerHTML — Cannot set property 'innerHTML' of null

我試圖根據由javascript函數填充的數字來更改div的innerHTML。 不幸的是,我遇到一個錯誤,我不確定為什么。

偽代碼

如果number > 2則將innerHTML更改為seconds否則將更改為second

我環顧四周,發現與DOM之前觸發的功能有關的東西。 我做了一些更改,但仍然沒有成功。 我做錯了什么? 渲染DOM后如何使函數觸發? window.onload已開始工作?

先感謝您

JS

$(document).ready(function() {
  window.onload = function() {
    // Month,Day,Year,Hour,Minute,Second
    upTime('may,05,2006,00:00:00');
  };

  function upTime(countTo) {
      var now = new Date();
      countTo = new Date(countTo);
      var difference = (now - countTo);

      var years = Math.floor(difference / (60 * 60 * 1000 * 24) / 365);
      var days = Math.floor(difference / (60 * 60 * 1000 * 24) * 1);
      var hours = Math.floor((difference % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
      var mins = Math.floor(((difference % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
      var secs = Math.floor((((difference % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);

      document.getElementById('years').firstChild.nodeValue = years;
      document.getElementById('days').firstChild.nodeValue = days;
      document.getElementById('hours').firstChild.nodeValue = hours;
      document.getElementById('minutes').firstChild.nodeValue = mins;
      document.getElementById('seconds').firstChild.nodeValue = secs;

      if (secs > 2) {
        console.log('over');
        document.getElementById('.seconds').innerHTML = "seconds";
      } else {
        console.log('lower');
        document.getElementById('.seconds').innerHTML = "second";
      }

      clearTimeout(upTime.to);
      upTime.to = setTimeout(function() {
          upTime(countTo);
      }, 1000);
  }
});

獲取元素: document.getElementsByClassName('seconds')ID document.getElementById('seconds')

但是使用jQuery, id $('#seconds')class $('.seconds')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM