简体   繁体   English

如何追加到div ID

[英]how to append to div id

i know it is easy and silly question but how can i show the lat and lng append into div id instead of giving me on alert.Can i do something like that !! 我知道这是一个简单而愚蠢的问题,但是如何显示lat和lng附加到div id中,而不是让我处于警戒状态。我可以做这样的事情!

document.getElementById("here").innerHTML = ("lat: +lat+"lng: " + lng) 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>

You may try something like this: Edit: Updated HTML 您可以尝试执行以下操作: 编辑:更新了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>

Ok So, I think what you want is this: 好的,我想您想要的是:

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

Thats how you append to your particular div. 那就是您如何附加到您的特定div。

是的,有可能,使用您编写的属性innerHTML应该可以解决您的问题。

Using jQuery you can simply do this: 使用jQuery,您可以简单地做到这一点:

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

Or if you want to keep the content of here and append the information at the end: 或者,如果您想保留here的内容并在末尾附加信息:

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

Yes, you definetely can. 是的,您绝对可以。 However, the string you provided is not quoted correctly. 但是,您提供的字符串未正确引用。 Don' t you get an exception in console? 您没有在控制台中遇到异常吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM