我有一个使用id向html元素添加文本的场景。但我收到了错误。
首先,html动态加载然后我使用id add variable
detail
添加到html元素
geocoder.geocode({
'location': temp1
}, function(results, status) {
if (status === 'OK') {
if (results[1]) {
add = results[1].formatted_address;
var marker0 = createMarker({
position: temp1,
map: map,
}, '<h1>Details</h1><p id="detail"></p>');
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
var geocoder = new google.maps.Geocoder;
var infoWindow = new google.maps.InfoWindow();
var add;
/*
* marker creater function (acts as a closure for html parameter)
*/
function createMarker(options, html) {
var marker = new google.maps.Marker(options);
if (html) {
google.maps.event.addListener(marker, "click", function() {
infoWindow.setContent(html);
console.log(add);
document.getElementById('detail').innerText = add;
infoWindow.open(options.map, this);
});
}
return marker;
}
Html被追加,但document.getElementById('detail')
给出了空值。
感谢任何帮助。