簡體   English   中英

如何追加到div ID

[英]how to append to div id

我知道這是一個簡單而愚蠢的問題,但是如何顯示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>

好的,我想您想要的是:

div = document.getElementById('here');
div.appendChild(watchId);

那就是您如何附加到您的特定div。

是的,有可能,使用您編寫的屬性innerHTML應該可以解決您的問題。

使用jQuery,您可以簡單地做到這一點:

$("#here").text("lat: " + lat + " lng: " + lng");

或者,如果您想保留here的內容並在末尾附加信息:

$("#here").append("lat: " + lat + " lng: " + lng");

是的,您絕對可以。 但是,您提供的字符串未正確引用。 您沒有在控制台中遇到異常嗎?

document.getElementById("here").innerHTML = "lat: " + lat + "lng: " + lng;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM