JavaScript upTime函数-不识别变量的ID
JavaScript upTime function - Not recognizing ID of variable
我的目标是让calcyear (cyear)最初从值"8"开始。每当天数达到364时,我希望从这个8值中减去一个值"1",直到它达到零。
由于某些原因,p id似乎不能识别日历id…或者它可能是,但是代码是错误的?
整个代码:<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>
问题出在这段代码上:
document.getElementById('years').firstChild.nodeValue = years;
在你的HTML中没有一个id为'years'
的元素,这样你会收到错误:
Uncaught TypeError: Cannot read property 'firstChild' of null
由于没有id为'years'
的元素,那么document.getElementById('years')
将返回null
。此错误还会停止执行,因此下一行将无法运行。因此,因为它是在calcyear
之后没有更新。
document.getElementById('years').firstChild.nodeValue = years; // Stops
document.getElementById('calcyear').firstChild.nodeValue = cyear; // Not ran
似乎你想添加它,但可能忘记了:
<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>
没有id为years
的元素,所以您的代码停在那里。
只要在你的代码中添加<p id="years">0</p>
,它就会运行。
相关文章:
- 如何检查名为id的变量属性
- 带有类和变量id的jquery选择器
- 获取<img>src,并通过class设置为变量(id不可用)
- JavaScript-更改一组包含特定文本和变量ID元素的链接
- Javascript 不会对带有变量 id 的复选框做出反应以显示隐藏的文本框
- 如何在jQuery中使用变量ID作为选择器
- jQuery:使用变量 ID 复制函数输出
- jQuery:变量 ID 未更新
- 使用表单中的PHP按钮根据变量ID从MYSQL数据库表中删除一行
- 在 JS 函数中调用的变量 ID
- 我可以在 html 输入标签元素中获取变量 ID 吗?
- 如何在jquery中的append函数中为html标签生成变量id
- JavaScript变量.Id作为按钮点击
- jQuery - Get <文本通过变量ID
- 通过函数将一个变量(id)从HTML传递到外部js文件
- 通过Javascript从表单传递一个变量id来打开一个新的file.html
- 具有类/变量ID的Javascript元素
- Jquery文件,变量id作为Ajax选择器
- 如何获取变量ID或类中的内容
- 基于变量“id”刷新多个iframe;从一个按钮