繁体   English   中英

jQuery更改html文本

[英]jQuery changing html text

一直试图在页面加载或页面保存时更改某些元素的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");
   }
});

您需要调用函数toLocaleString ,而不是仅将其作为对html()方法的引用传递。

var d = new Date();
$('.timestamp').each(function () {
    $(this).html(d.toLocaleString());
});

演示: 小提琴

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");
    }
});

也许我没有理解这个问题,但我想你想要每秒更新一次日期......

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);
var d = new Date();
$('.timestamp').each(function () {
    $(this).html(d.toLocaleString());  //toLocaleString is a method so close with ()
});

这段代码可行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM