如何讓網頁顯示時間和地點 網頁怎么設置顯示所有內容
Zilingo時尚前沿跨境問答2025-05-294660
要讓網頁顯示時間和地點,可以使用HTML和JavaScript來實現(xiàn)。以下是一個簡單的示例:
在HTML文件中添加一個
<script>
標簽,用于引入JavaScript代碼。在
<head>
標簽內添加以下CSS樣式:
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
</style>
- 在
<body>
標簽內添加以下JavaScript代碼:
function getCurrentTimeAndLocation() {
const currentTime = new Date();
const location = new Position(currentTime);
const formattedTime = currentTime.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
const formattedLocation = location.toFormattedString();
return `當前時間:${formattedTime},位置:${formattedLocation}`;
}
document.addEventListener('DOMContentLoaded', () => {
const timeAndLocation = getCurrentTimeAndLocation();
document.getElementById('timeAndLocation').innerText = timeAndLocation;
});
- 在HTML文件中添加一個
<div>
標簽,用于顯示時間和地點信息:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>顯示時間和地點</title>
<link rel="stylesheet" />
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
</head>
<body>
<div id="timeAndLocation"></div>
<script src="main.js"></script>
</body>
</html>
- 最后,將上述代碼保存為一個名為
main.js
的文件,并將其與HTML文件放在同一目錄下。運行HTML文件,即可看到網頁顯示時間和地點的信息。
本文內容根據網絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉載請注明,如有侵權,聯(lián)系刪除。