尝试在页面加载或页面保存时更改某些元素的innerHTML。
基本上,当我保存HTML页面时,我希望时间戳只更新一次,而不是页面加载或任何东西......这样人们可以看到状态上次更新的时间。
我也想拥有它所以我需要做的就是将#status
的类更改为close
,当页面加载时它会自动更改div中的文本。
这是我的小提琴:http://jsfiddle.net/MD3Wx/1/
HTML
<div class="lastChange">
This was last updated on: <span class="timestamp">00:00:00</span>
</div>
<div class="close" id="status">
OPEN
</div>
<hr />
<div class="comments">
Comments :<br />
<span class="timestamp">00:00:00</span> <span class="info">Info</span></div>
JQUERY
var d = new Date();
$('.timestamp').each(function() {
$(this).html(d.toLocaleString);
});
$(document).ready(function(){
if($('#status').hasClass("open")){
$('#status').html("OPEN");
}
else{
$('#status').html("CLOSED");
}
});
答案 0 :(得分:1)
您需要调用函数toLocaleString
,而不是仅仅将其作为html()
方法的引用传递。
var d = new Date();
$('.timestamp').each(function () {
$(this).html(d.toLocaleString());
});
演示:Fiddle
答案 1 :(得分:1)
var d = new Date();
$('.timestamp').each(function () {
$(this).html(d.toLocaleString()); //toLocaleString is a method so close with ()
});
$(document).ready(function () {
if ($('#status').hasClass("open")) {
$('#status').html("OPEN");
} else {
$('#status').html("CLOSED");
}
});
答案 2 :(得分:0)
也许我没有理解这个问题,但我认为你想要每秒更新一次日期......
setInterval(function(){
var d = new Date();
$('.timestamp').each(function() {
$(this).html(d.toLocaleString());
});
$(document).ready(function(){
if($('#status').hasClass("open")){
$('#status').html("OPEN");
}
else{
$('#status').html("CLOSED");
}
});
}, 1000);
答案 3 :(得分:0)
var d = new Date();
$('.timestamp').each(function () {
$(this).html(d.toLocaleString()); //toLocaleString is a method so close with ()
});
此代码可以使用。