我一直在這段代碼上獲得一個定時器的地理定位10秒,結果將顯示在文本區域。問題是如何在不替換舊的結果的情況下添加新的結果,並且如果需要可以自動擴展textarea。html追加到textarea
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML that display geolocation</title>
</head>
<body>
<textarea rows="50" cols="100" id="demo"></textarea>
<script>
var x = document.getElementById("demo");
var timer = setInterval(function() { getLocation() }, 10000);
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(success, error)
}
else
{
x.innerHTML = "Geoloaction is not supported."
}
}
function success(pos)
{
var y = pos.coords;
var z = new Date().toLocaleTimeString();
x.innerHTML = z + " Latitude: " + y.latitude + " Longitude" + y.longitude;
}
function error(err)
{
switch (error.code)
{
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
</body>
</html>
感謝您的回答,+ =絕對有幫助。如果我想在每個條目的末尾添加換行符,會發生什麼情況,因此它們都像列表一樣排列。感謝您的投入。 – Jerry