无法设置null的属性'textcontent'

时间:2017-12-22 18:36:09

标签: javascript html html5

由于某些原因我得到错误:无法为此行设置null的属性'textcontent':

 document.getElementById("days").textContent = d;

function countdown() {
  var now = new Date();
  var eventDate = new Date(2018, 1, 19);
  var currentTime = now.getTime();
  var eventTime = eventDate.getTime();
  var remTime = eventTime - currentTime;
  var s = Math.floor(remTime / 1000);
  var m = Math.floor(s / 60);
  var h = Math.floor(m / 60);
  var d = Math.floor(h / 24);
  h %= 24;
  m %= 60;
  s %= 60;
  h = (h < 10) ? "0" + h : h;
  m = (m < 10) ? "0" + m : m;
  s = (s < 10) ? "0" + s : s;

  document.getElementById("days").textContent = d;
  document.getElementById("days").innerText = d;

  document.getElementById("hours").textContent = h;

  document.getElementbyId("minutes").textContent = m;

  document.getElementById("seconds").textContent = s;
}
countdown();
<li>
  <span class="value">Ends in:
    </span>
  <p id="days">30</p>
  <p id="hours">10</p>
  <p id="minutes">5</p>
  <p id="seconds">1</p>
</li>

3 个答案:

答案 0 :(得分:0)

问题可能是您的&#34;倒计时&#34;运行...你可以尝试调用包含你要查询的元素集的body / div的倒计时onload吗?

<body onload="countdown()">

答案 1 :(得分:0)

我看到的唯一问题是您的b需要在此行中为大写:

document.getElementbyId("minutes").textContent = m;

修复代码按预期工作时:

function countdown() {
  var now = new Date();
  var eventDate = new Date(2018, 1, 19);
  var currentTime = now.getTime();
  var eventTime = eventDate.getTime();
  var remTime = eventTime - currentTime;
  var s = Math.floor(remTime / 1000);
  var m = Math.floor(s / 60);
  var h = Math.floor(m / 60);
  var d = Math.floor(h / 24);
  h %= 24;
  m %= 60;
  s %= 60;
  h = (h < 10) ? "0" + h : h;
  m = (m < 10) ? "0" + m : m;
  s = (s < 10) ? "0" + s : s;

  document.getElementById("days").textContent = d;
  document.getElementById("days").innerText = d;

  document.getElementById("hours").textContent = h;

  document.getElementById("minutes").textContent = m;

  document.getElementById("seconds").textContent = s;
}
countdown();
<li>
  <span class="value">Ends in:
    </span>
  <p id="days">30</p>
  <p id="hours">10</p>
  <p id="minutes">5</p>
  <p id="seconds">1</p>
</li>

确保在dom加载完毕后运行代码。

你可以这样做:

document.addEventListener('DOMContentLoaded', () => countdown());

答案 2 :(得分:0)

您出现错误:getElementById而不是getElementbyId

新代码将是:

function countdown() {
  var now = new Date();
  var eventDate = new Date(2018, 1, 19);
  var currentTime = now.getTime();
  var eventTime = eventDate.getTime();
  var remTime = eventTime - currentTime;
  var s = Math.floor(remTime / 1000);
  var m = Math.floor(s / 60);
  var h = Math.floor(m / 60);
  var d = Math.floor(h / 24);
  h %= 24;
  m %= 60;
  s %= 60;
  h = (h < 10) ? "0" + h : h;
  m = (m < 10) ? "0" + m : m;
  s = (s < 10) ? "0" + s : s;

  document.getElementById("days").textContent = d;
  document.getElementById("days").innerText = d;

  document.getElementById("hours").textContent = h;

  document.getElementById("minutes").textContent = m;

  document.getElementById("seconds").textContent = s;
}
countdown();
<li>
  <span class="value">Ends in:
    </span>
  <p id="days">30</p>
  <p id="hours">10</p>
  <p id="minutes">5</p>
  <p id="seconds">1</p>
</li>