简体   繁体   English

为什么我的 Javascript 代码不能访问 google.maps.InfoWindow 中的 div?

[英]Why can't my Javascript code access a div inside a google.maps.InfoWindow?

I'm using the Google Maps Javascript API.我正在使用 Google Maps Javascript API。 I'm setting the content of an InfoWindow to be a div.我将 InfoWindow 的内容设置为 div。 I then try to use document.getElementById() to get the div element in the InfoWindow.然后我尝试使用 document.getElementById() 来获取 InfoWindow 中的 div 元素。

  var contentString = '<div id="content" style="width:200px;height:200px;"></div>';

  var infowindow = new google.maps.InfoWindow({
      position: map.getCenter(),
      content: contentString
    });

  infowindow.open(map);

  console.log(document.getElementById('content'));

The InfoWindow shows up at the center of the map as I expect.正如我所料,信息窗口显示在地图的中心。 However, the last line of my code here logs 'null' to the console.但是,此处代码的最后一行将“null”记录到控制台。 If I execute the same line of code within a different function after this code completes, an element is returned from getElementById('content') and info about the element is logged to the console.如果我在此代码完成后在不同的函数中执行同一行代码,则会从 getElementById('content') 返回一个元素,并将有关该元素的信息记录到控制台。 I don't understand what is happening here.我不明白这里发生了什么。 I'd think that immediately after the code lines that create the InfoWindow and open it on the map that the 'content' div is part of the DOM.我认为,在创建 InfoWindow 并在地图上打开它的代码行之后,“内容”div 是 DOM 的一部分。 But running my code says otherwise.但是运行我的代码则不然。 Anyone understand what's going wrong?任何人都明白出了什么问题?

The infowindow content is added to the DOM asynchronously. infowindow 内容被异步添加到 DOM。 It is not available to get using .getElementById until after it is in the DOM..getElementById进入 DOM 之前,它无法使用。 To detect when it has been added to the DOM use the domready event on the infowindow:要检测它何时被添加到 DOM,请使用信息窗口上的domready事件:

domready Arguments: None domready参数:无

This event is fired when the containing the InfoWindow's content is attached to the DOM.当包含 InfoWindow 内容的 DOM 附加到 DOM 时,将触发此事件。 You may wish to monitor this event if you are building out your info window content dynamically.如果您正在动态构建信息窗口内容,您可能希望监视此事件。

var contentString = '<div id="content" style="width:200px;height:200px;">14</div>';

var infowindow = new google.maps.InfoWindow({
  position: map.getCenter(),
  content: contentString
});

infowindow.open(map);
google.maps.event.addListener(infowindow, 'domready', function() {
    console.log(document.getElementById('content'));
});

proof of concept fiddle概念证明小提琴

code snippet:代码片段:

 function initialize() { var map = new google.maps.Map( document.getElementById("map_canvas"), { center: new google.maps.LatLng(37.4419, -122.1419), zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP }); var contentString = '<div id="content" style="width:200px;height:200px;">14</div>'; var infowindow = new google.maps.InfoWindow({ position: map.getCenter(), content: contentString }); infowindow.open(map); google.maps.event.addListener(infowindow, 'domready', function() { console.log(document.getElementById('content')); }); } google.maps.event.addDomListener(window, "load", initialize);
 html, body, #map_canvas { height: 100%; width: 100%; margin: 0px; padding: 0px }
 <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script> <div id="map_canvas"></div>

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

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