我有一个谷歌地图应用程序,点击地图上的一个地区会创建信息窗口。
下面是我的代码结构:...
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(MapArea, "click", showTB);
function showTB () {
...
contentElement = //complicated table with buttons etc
infowindow.setContent(sContentString);
infowindow.setPosition(event.latLng);
infowindow.open(map);
//contentElement dom element manipulations, such as:
$("#table_row").css({background: "#8af"});
}
这里的问题是末尾的dom元素操作通常不执行。我的猜测是infowindow.open(map)
是异步的,并且它通常只在dom元素操作部分之后完成执行,因为html元素还不存在,所以没有执行。在infowindow.open(map)
之后放置一个警告语句通常会导致最后一段代码执行,假设是由于延迟造成的。
所以我怎么能确保任何代码只执行后infowindow.open()
已经完成执行?
我能让它同步吗?还有别的办法吗?我不只是想添加延迟函数,因为那样要么很慢要么不可靠。
等待infowindow上的'domready'事件
代码:domready |无|当包含InfoWindow的内容时触发此事件附加到DOM。如果您正在构建您的动态信息窗口内容
infowindow.setContent(sContentString);
infowindow.setPosition(event.latLng);
infowindow.open(map);
google.maps.event.addListener(infowindow,'domready', function() {
//contentElement dom element manipulations, such as:
$("#table_row").css({background: "#8af"});
});