W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>JavaScript 地理位置</h1> <p>请单击按钮以获取您的坐标。</p> <button onclick="getLocation()">试一试</button> <p id="demo"></p> <script> const x = document.getElementById("demo"); function getLocation() { try { navigator.geolocation.getCurrentPosition(showPosition); } catch { x.innerHTML = err; } } function showPosition(position) { x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; } </script> </body> </html>