2014-02-14 80 views
0

一直在努力改變一些元素無論是在頁面加載或保存網頁的innerHTML。jQuery的改變HTML文本

基本上當我保存的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"); 
    } 
}); 
+1

它應該是'$(this).html(d.toLocaleString());' - http://jsfiddle.net/arunpjohny/5WNAR/1/ - 因爲'toLocaleString'是一種方法 - 你還有什麼想擁有隻是固定的一切......哈哈哈愚蠢的錯誤 –

+0

@ArunPJohny好。我原本是使用JavaScript和innerHTML的,我有它在那裏,但是當我改變了它在我忘記了括號。 – Adjit

回答

1

你需要調用函數toLocaleString,而不是僅僅把它當作參考方法html()

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

演示:Fiddle

+0

是否有我可以有日期,以便它只在我保存HTML頁面時發生變化,因爲從現在開始每次刷新頁面的時間都會發生變化,這對我根本沒有任何幫助。 – Adjit

+0

如果您想要評論時間,那麼您通過保存頁面 –

+0

意味着什麼,那麼您無法在客戶端執行此操作,您需要將時間與評論一起存儲在服務器中,並在顯示時反饋給您需要與評論一起獲取時間從服務器 –

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

此代碼將起作用。