如何设置 <input> 的值而不是 <p> 的 innerHTML?
How do I set the value of an <input> rather than the innerHTML of a <p>?
我想把<p id="demo"></p>
改成<input type="text" id="demo">
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
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>
而不是设置 x.innerHTML
你会设置 x.value
:
<button onclick="getLocation()">Try It</button>
<input type="text" id="demo" />
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
x.value = position.coords.latitude + "," + position.coords.longitude;
}
</script>
我想把<p id="demo"></p>
改成<input type="text" id="demo">
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
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>
而不是设置 x.innerHTML
你会设置 x.value
:
<button onclick="getLocation()">Try It</button>
<input type="text" id="demo" />
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
x.value = position.coords.latitude + "," + position.coords.longitude;
}
</script>