北京時(shí)間顯示時(shí)分秒在線顯示 北京時(shí)間顯示北京時(shí)間表
Fruugo環(huán)球購(gòu)賣(mài)家服務(wù)2025-06-103801
要實(shí)現(xiàn)北京時(shí)間顯示時(shí)分秒在線顯示,可以使用JavaScript編寫(xiě)一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)。在HTML中添加一個(gè)<input>
元素用于輸入時(shí)間,以及一個(gè)<button>
元素用于觸發(fā)時(shí)間的更新。然后,使用JavaScript監(jiān)聽(tīng)按鈕的點(diǎn)擊事件,獲取輸入的時(shí)間,并更新顯示時(shí)間的元素。
以下是一個(gè)簡(jiǎn)單的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>北京時(shí)間顯示</title>
</head>
<body>
<input type="text" id="timeInput" placeholder="請(qǐng)輸入時(shí)間(時(shí)分秒)">
<button onclick="updateTime()">更新時(shí)間</button>
<p id="displayTime"></p>
<script>
function updateTime() {
const timeInput = document.getElementById("timeInput");
const displayTime = document.getElementById("displayTime");
const timeValue = timeInput.value;
if (timeValue) {
const [hours, minutes, seconds] = timeValue.split(":").map(Number);
if (isNaN(hours) || isNaN(minutes) || isNaN(seconds)) {
alert("輸入格式錯(cuò)誤,請(qǐng)重新輸入!");
return;
}
hours = Math.max(0, Math.min(23, hours));
minutes = Math.max(0, Math.min(59, minutes));
seconds = Math.max(0, Math.min(59, seconds));
displayTime.innerHTML = `${hours}:${minutes}:${seconds}`;
} else {
displayTime.innerHTML = "";
}
}
</script>
</body>
</html>
將以上代碼保存為一個(gè)HTML文件,然后用瀏覽器打開(kāi)即可看到效果。用戶可以通過(guò)輸入時(shí)間(時(shí)分秒)來(lái)實(shí)時(shí)查看北京時(shí)間。
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。