我試圖通過頁面加載自動通過html的地理定位座標,但它不工作。爲什麼函數無法在Document load上執行?
<body >
<p id="demo">hi</p>
</body>
<script>
$(document).ready(function() {
var x = document.getElementById("demo");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
});
</script>
任何幫助,非常感謝。
https://jsfiddle.net/bushido/dozay2mo/
檢查答案(和音符,它們很重要!) – Dekel