我有一个具有innerhtml函数的javascript代码,例如
bool enabled
我想输入上面的javascript代码的输出,并输入到输入文本表单值中
<script>
var x = document.getElementById("gps");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = position.coords.latitude +
"," + position.coords.longitude;
}
</script>
但是我无法获得想要的结果,是否需要添加或更改代码?
答案 0 :(得分:1)
您需要调用函数。
假设<script>
块位于页面底部,<input>
呈现后,这应该可以工作:
<script>
var x = document.getElementById("gps");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = position.coords.latitude +
"," + position.coords.longitude;
}
getLocation();
</script>