首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaScript upTime函数-无法识别变量的ID

JavaScript upTime函数-无法识别变量的ID
EN

Stack Overflow用户
提问于 2016-07-06 09:02:12
回答 2查看 68关注 0票数 0

我的目标是calcyear (cyear)最初从值"8“开始。每当天数达到364,我希望从这个8值中减去一个值"1“,直到它达到零。

由于某些原因,p id似乎无法识别calcyear id...或者也许是这样,但是代码是错误的?

完整代码:

代码语言:javascript
运行
复制
<html>
<style>
#countup p {
display: inline-block;
padding: 0px;
margin: 0 0 10px;
}
#paragraph2 p {
display: inline-block;
padding: 0px;
margin: 0 0 10px;
}
</style>
<div id="countup">
  We are in day
  <p id="days">00</p>
  <p class="timeRefDays">of the calendar. This day has been going on for </p>
  <p id="hours">00</p>
  <p class="timeRefHours">hours, </p>
  <p id="minutes">00</p>
  <p class="timeRefMinutes">minutes, and </p>
  <p id="seconds">00</p>
  <p class="timeRefSeconds"> seconds.</p>
</div>

<div id="paragraph2">
<p>We are in month</p>
<p id="months">00</p>
<p>of the year.</p>
</div>

<div id="paragraph2">
<p>In </p>
<p id="calcyear">0</p>
<p> years an intercalation week will be added.</p>
</div>

<script>
window.onload=function() {
  upTime('mar,20,2016,00:00:00'); 
}
function upTime(countTo) {
  now = new Date();
  countTo = new Date(countTo);
  difference = (now-countTo);

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

  cyear= 8
  years=Math.floor(days / 364)
  if (years > 1){ cyear = cyear - 1}

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


  clearTimeout(upTime.to);
  upTime.to=setTimeout(function(){ upTime(countTo); },1000);
}
</script>
</html>
EN

回答 2

Stack Overflow用户

发布于 2016-07-06 09:27:22

没有id为years的元素,所以您的代码到此为止。

只需将<p id="years">0</p>添加到您的代码中,它就会运行。

票数 0
EN

Stack Overflow用户

发布于 2016-07-06 09:27:41

问题出在下面这段代码上:

代码语言:javascript
运行
复制
document.getElementById('years').firstChild.nodeValue = years;

在你的超文本标记语言中没有id为'years'的元素,因此你会收到错误信息:

未捕获TypeError:无法读取null的属性'firstChild‘

由于不存在id为'years'的元素,因此document.getElementById('years')将返回null。此错误还会停止执行,因此下一行将不会运行。所以因为它是在那个calcyear没有更新之后。

代码语言:javascript
运行
复制
document.getElementById('years').firstChild.nodeValue = years; // Stops
document.getElementById('calcyear').firstChild.nodeValue = cyear; // Not ran

您似乎打算添加它,但可能忘记了:

代码语言:javascript
运行
复制
<div id="paragraph2">
<p>We are in month</p>
<p id="months">00</p>
<p>of the year </p>
<p id="years">00</p>  <!-- Added line -->
</div>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38215005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档