[英]Is it possible to current time dynamicly displayed in web pages in chrome browser?
我需要的是在chrome瀏覽器訪問的網頁中動態顯示當前時間,就像將其插入原始網頁一樣,或者可以將其顯示為背景...?
我不知道您到底想做什么...但是可以使用Date
對象輕松讀取當前時間。 創建沒有任何參數的新Date
對象將導致當前時間Date
對象。
要將其插入頁面,您可以執行以下操作:
// Create a div and append it to the <body>
var div = document.createElement("div");
div.id = "time";
document.body.appendChild(div);
function clock() {
var now = new Date(),
h = now.getHours(),
m = now.getMinutes(),
s = now.getSeconds();
// Put the current time (hh:mm:ss) inside the div
div.textContent =
(h>9 ? "" : "0") + h + ":" +
(m>9 ? "" : "0") + m + ":" +
(s>9 ? "" : "0") + s;
}
// Execute clock() every 1000 milliseconds (1 second)
setInterval(clock, 1000);
上面的代碼將在頁面內插入一個div,並以當前時間每秒更新其文本,例如時鍾。 現在,您應該將其設置為始終可見,如下所示:
#time {
position: fixed;
z-index: 999999999;
top: 0;
left: 0;
}
上面的CSS會將元素固定在頁面的左上角。 您可以根據需要設置樣式,然后將其移至頁面的其他部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.