【问题标题】:how to append to div id如何附加到 div id
【发布时间】:2014-04-27 20:57:49
【问题描述】:

我知道这是一个简单而愚蠢的问题,但是我怎样才能将 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>

【问题讨论】:

  • 您在问如何使用您制作的 id 制作 div?或者如何将一些东西附加到一个div?因为我没有看到正在创建的 div。
  • watchId 是您想要的 div id 吗?
  • 追加到 内的 div。
  • document.getElementById("here").innerHTML = "lat: " + lat + " lng: " + lng;
  • 是的brendon .. 那就是watchId,它显示了当前位置的权利...

标签: javascript html


【解决方案1】:

您可以尝试这样的操作:编辑:更新的 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>

【讨论】:

    【解决方案2】:

    好吧,我想你想要的是这个:

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

    这就是附加到特定 div 的方式。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        是的,你绝对可以。但是,您提供的字符串未正确引用。控制台中没有异常吗?

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

        【讨论】:

        • 不,老实说看起来不错。你运行它但它没有工作?
        • 这个? document.getElementById("here").innerHTML = ("lat: +lat+"lng: " + lng)? 至少它有奇数个引号...
        【解决方案5】:

        使用jQuery,您可以简单地做到这一点:

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

        或者如果您想保留here的内容并在末尾附加信息:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-30
          • 2011-08-06
          • 2021-04-18
          相关资源
          最近更新 更多