字符串str总是空的,尽管它填充了.geojson文件中的数据

the string str is always empty although it is filled with data from the.geojson file any idea?

本文关键字:填充 geojson 文件 数据 str 字符串      更新时间:2023-09-26

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script>
    $(document).ready(function() {
      $("#p1").load("reporting/data.geojson").toString();
      var str = document.getElementById("p1").innerHTML;
      document.getElementById("p2").innerHTML = str;
    });
  </script>
</head>
<body>
  <div id="div1">
    <h2>try</h2>
  </div>
  <p id="p1"></p>
  <p id="p2"></p>
</body>
</html>

这是代码,p1清楚地显示在屏幕上,但我的实际问题是我无法用它填充字符串,"也许加载函数是最后一个操作的,我不知道"对此有点新。我需要把.geojson文本放在一个字符串中,或者任何其他提取坐标的方法都可以省去字符串edding的麻烦。感谢iij预付

您需要使用load回调#p1获取数据。

load方法是异步的,因此代码不等待数据加载并执行下一条语句。

$(document).ready(function() {
  $("#p1").load("reporting/data.geojson", function() {
    var str = document.getElementById("p1").innerHTML;
    document.getElementById("p2").innerHTML = str;
  });
});

当您使用jQuery时,您可以使用html()

$(document).ready(function () {
    $("#p1").load("reporting/data.geojson", function () {
        $('#p2').html($('#p1').html());
    });
});