jQuery日期和时间不修改HTML文档
这段代码似乎没有任何作用。。。没有错误。。。 它已在“头”中链接jQuery日期和时间不修改HTML文档,jquery,html,twitter-bootstrap,Jquery,Html,Twitter Bootstrap,这段代码似乎没有任何作用。。。没有错误。。。 它已在“头”中链接 $(document).ready(function() { function currentDate() { var now = new Date(); var year = now.getFullYear(); var month = now.getMonth(); var day = now.getDay(); var hours = now.getHours(); var
$(document).ready(function() {
function currentDate() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDay();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var time = hours + ":" + minutes + ":" + seconds;
var date = (day + "/" + month + "/" + year).toLocaleDateString();
$('#time').html("<span class='glyphicon glyphicon-time'></span> " + time);
$('#date').html("<span class='glyphicon glyphicon-calendar'></span> " + date);
}
currentDate();
setInterval(currentDate(), 1000);
});
我的要点是:
<script src="js/datetime.js"></script>
网页似乎没有显示任何内容,在它应该去的地方
setInterval应采用函数变量。但是在你的例子中,你调用了这个函数。不应使用大括号:
<div class="navbar-right" style="color:#fff;margin-right:0px;">
<div class="navbar-brand" id="fade">
<p id="date" title="Your Date."></p>
</div>
<div class="navbar-brand" id="fade">
<p id="time" title="Your Time."></p>
</div>
</div>
二,
您有重复的id id=淡入淡出。使用唯一的ID。.toLocaleDateString不能仅用于日期字符串。可以将toLocaleDateString添加到新日期。如果使用setInterval,则必须调用不带括号的函数
setInterval(currentDate, 1000);
您确定js/datetime.js是文件的正确路径吗。尝试html中的代码:请不要只发布代码答案。请解释你的答案。我用一些解释编辑了我的帖子
function currentDate() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDay();
var hours = now.getHours();
var minutes = now.getMinutes();
minutes = (minutes < 10) ? '0'+minutes : minutes;
var seconds = now.getSeconds();
seconds = (seconds < 10) ? '0'+seconds : seconds;
var time = hours + ":" + minutes + ":" + seconds;
var date = (day + "/" + month + "/" + year);
$('#time').html("<span class='glyphicon glyphicon-time'></span> " + time);
$('#date').html("<span class='glyphicon glyphicon-calendar'></span> " + date);
}
$(function() {
currentDate();
setInterval(currentDate, 1000);
});