[英]Javascript googlemap api document.write() doesn't work
<script
src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var geocoder = new google.maps.Geocoder();
<%for (String st : t.keySet()) {%>
printGeocode( <%=t.get(st)[1]%> , <%=t.get(st)[0]%> );
<%}%>
function printGeocode(x, y) {
document.write("Test Check1<br>"); // work fine
alert("test Check1"); // work fine
var latlng = new google.maps.LatLng(x, y);
geocoder.geocode({
'latLng' : latlng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
document.write(results[1].formatted_address+"<br>"); // didn't work
alert(results[1].formatted_address); // Work Fine
} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
}
</script>
this is my source 这是我的来源
alert(String) is work fine alert(String)正常工作
but document.write is doesn't work 但是document.write不起作用
what is different between first " document.write("Test Check1<br>"); // work fine
" 第一个“ document.write("Test Check1<br>"); // work fine
之间的区别是什么?
and document.write(results[1].formatted_address+"<br>");
和document.write(results[1].formatted_address+"<br>");
i can't understand `alert(results[1].formatted_address); // Work Fine`
appear alert address why 出现警报地址的原因
document.write(results[1].formatted_address+"<br>"); // didn't work
didn't appear on webpage? 没有出现在网页上?
The document.write()
API is only safe to use while the DOM is being built by the browser. 仅当浏览器正在构建DOM时,才能安全使用document.write()
API。 Using it in a callback from the maps API means that by the time it runs, the DOM has been finalized. 在maps API的回调中使用它意味着到它运行时,DOM已经完成。 At that point, a call to document.write()
means implicitly that the DOM should be wiped out and a new one prepared. 那时,对document.write()
的调用暗含意味着应该清除DOM并准备一个新的DOM。
If you need to add content, use the DOM manipulation APIs and/or innerHTML
. 如果您需要添加内容,请使用DOM操作API和/或innerHTML
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.