我有以下代码:
var infoWindow = new google.maps.InfoWindow(),
content = "<div class=\"row\">"+
"<h2 class=\"col xs4\">"+name+"</h2>"+
"<div class=\"col xs2\">"+
"<div id=\"w108\"></div>"+
"</div>"+
"<div class=\"col xs2\">"+
"<div id=\"o108\"></div>"+
"</div>"+
"<div class=\"col xs2\">"+
"<div id=\"c108\"></div>"+
"</div>"+
"<div class=\"col xs2\">"+
"<div id=\"i108\"></div>"+
"</div>"+
"<div class=\"col xs2\">"+
"<div id=\"pi108\"></div>"+
"</div>"+
"<div class=\"col xs2\">"+
"<div id=\"a108\"></div>"+
"</div>"+
"<div class=\"col xs2\">"+
"<div id=\"bi108\"></div>"+
"</div>"+
"<div class=\"col xs2\">"+
"<div id=\"dw108\"></div>"+
"</div>"+
"</div>";
infoWindow.setContent(content);
var parent = document.createElement("div"),
title = document.createElement("h1"),
t = document.createTextNode(name),
w = document.getElementById("w108"),
wChart = new google.charts.Bar(w),
o = document.getElementById("o108"),
oChart = new google.charts.Bar(o),
c = document.getElementById("c108"),
cChart = new google.charts.Bar(c),
i = document.getElementById("i108"),
iChart = new google.charts.Bar(i),
pi = document.getElementById("pi108"),
piChart = new google.charts.Bar(pi),
a = document.getElementById("a108"),
aChart = new google.charts.Bar(a),
bi = document.getElementById("bi108"),
biChart = new google.charts.Bar(bi),
dw = document.getElementById("dw108"),
dwChart = new google.charts.Bar(dw);
问题在于,例如调用dw = document.getElementById("dw108"), dwChart = new google.charts.Bar(dw)
时,div不会成为jet。这会导致错误:
未捕获错误:未定义容器。
我已经尝试将infoWindow.setContent(content);
置于不同的行,但这并没有帮助。在div选择变量周围使用setTimeout
也失败了。所以我的问题是如何在仍然需要创建div dw108
时将其作为目标?
答案 0 :(得分:1)
您有两种选择。
domready
事件侦听器内)选择该元素
醇>
来自文档:
domready |参数:无 当包含InfoWindow的内容附加到DOM时会触发此事件。如果要动态构建信息窗口内容,您可能希望监视此事件。
示例:
infoWindow.setContent(content);
google.maps.event.addListener(infoWindow, "domready", function() {
// What needs to be done after domready
});
InfoWindow.setContent
方法(带字符串或Node (DOM ELement))。setContent(含量:串|节点)
返回值:无