我知道这是一个简单而愚蠢的问题,但是如何显示lat和lng附加到div id中,而不是让我处于警戒状态。我可以做这样的事情!

document.getElementById(“ here”)。innerHTML =(“ lat:+ lat +” lng:“ + lng)

<!DOCTYPE>
<html>
<meta name="viewport" content="width=device-width,
initial-scale=1, maximum-scale=1,user-scalable=no">
<head>


    <script>
        function geolocation(){
<!--checks if geolocation is available -->
var options = { enableHighAccuracy: true};
watchId = navigator.geolocation.watchPosition(onSuccess, onError, options);
        <!-- function run if gets the geolocation back -->
function onSuccess(position){
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;


    alert("lat: " + lat + "lng: " + lng);

    }
    <!-- this function run if there is any error in geolocation -->
function onError(error){
    alert("message: " + error.message);

                }
        }
        geolocation();

    </script>

</head>
<body>


    <div id="here"></div>



</body>

最佳答案

您可以尝试执行以下操作:编辑:更新了HTML

<div id="location"></div>

    <script>

        $( document ).ready(function() {
            geolocation();
        }

        function geolocation(){
            <!--checks if geolocation is available -->
            var options = { enableHighAccuracy: true};
            watchId = navigator.geolocation.watchPosition(onSuccess, onError, options);
                <!-- function run if gets the geolocation back -->
            function onSuccess(position){
                var lat = position.coords.latitude;
                var lng = position.coords.longitude;

                //alert("lat: " + lat + "lng: " + lng);
                document.getElementById("location").innerHTML = "lat: " + lat + "lng: " + lng;
            }

            <!-- this function run if there is any error in geolocation -->
                function onError(error){
                alert("message: " + error.message);
            }
        }

    </script>

关于javascript - 如何追加到div ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23328900/

10-12 04:23