返回innerHtml的函数的结果是未定义的,原因是什么

result of function that returns innerHtml is undefined, why

本文关键字:是什么 结果是 innerHtml 函数 返回 未定义      更新时间:2023-09-26
<html>
<body>
<p id="GeoElem">
</p>

//当按下这个按钮时,我应该重定向到在Func2 中计算的值的url

<button id="myButton2" onClick=myFunct3()> Click me </button> 
<script>

//构建我的url的函数;Func2返回未定义的

function myFunc3() { 
        window.location.href = window.location.href + "/search/" + Func2();
    }

//需要Func2调用Geo,Geo将位置计算到元素的内部html中

function Func2() { 
Geo(); 
var x = document.getElementById("GeoElem"); 
return strip(x.innerHTML); //return the innerHTML
}

//应该将位置存储在名为GeoElem 的元素的innerHTML中

function Geo() { 
               var x = document.getElementById("GeoElem");
               function showPosition(position) {
                   x.innerHTML = position.coords.latitude + ";" + position.coords.longitude;
               }
               if (navigator.geolocation) {
                   x.innerHTML = navigator.geolocation.getCurrentPosition(showPosition);
               }
               else {
                   x.innerHTML = "Geolocation is not supported by this browser.";
               }
                //return x.innerHTML;
}

//该函数应该返回纯文本,去掉html

function strip(html)
{
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}
</script>
</body>
</html>

===============证明我的主要功能(Geo(有效:。。。。然而,当您像上面的例子一样把所有东西放在一起时,它会返回未定义的url,而不是坐标。

这是一个函数示例,它在html元素中返回屏幕上的坐标,但我希望它们由上面尝试的函数返回

<p id="idx"></p>
<script>
        var x = document.getElementById("idx");
               function showPosition(position) {
                   x.innerHTML = position.coords.latitude + ";" + position.coords.longitude;
               }
               if (navigator.geolocation) {
                   x.innerHTML = navigator.geolocation.getCurrentPosition(showPosition);
               }
               else {
                   x.innerHTML = "Geolocation is not supported by this browser.";
               }
</script>

注意。最好是让Geo函数直接返回坐标,而不将它们存储到元素的innerHTML中,但这不起作用:

function handle_geolocation_query() {
            navigator.geolocation.getCurrentPosition(function (position) {
                var position = {
                    lat: position.coords.latitude,
                    lng: position.coords.longitude
                };
                var lat = position.lat;
                var long = position.lng;
                return (lat + ';' + long);
            })
        }

两种方法中的任何一种都可以。要么直接计算它并从函数返回,要么通过html元素抓取它并像myFunct3 中的window.location.href = window.location.href + "/search/" + handle_geolocation_query();一样传递它

function myFunction() {
  var x = document.getElementById("id22");
  function showPosition(position) { //When the user allows Geolocation, you could get the position, otherwise you can't
    x.innerHTML = position  !== undefined ? position.coords.latitude + ";" + position.coords.longitude : "You don't have allow Geo";
  }
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);  //At this point, the Geolocation is ever undefined
    x.innerHTML = 'Please allow Geo!';
  }
  else {
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

函数navigator.geolocation.getCurrentPosition不返回任何内容(另请参阅https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition)因此默认情况下,它返回undefined。

希望有帮助。