我使用的是Google Maps Javascript API,我希望這樣做的目的是當您點擊地圖上的按鈕時,會彈出JS警報。Google Maps API addListener方法不起作用
這裏是我的JS代碼至今:
var gmap = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// Setting up and adding the boundary-changing buttons
var boundaryChangeArr = [
'<h3 id="boundary-change-title">Change boundaries</h3>',
'<div id="boundary-change-buttons-container">',
'<input id="boundaries-states" class="boundary-change-button" type="button" name="StatesBoundaries" value="States" />',
'<input id="boundaries-counties" class="boundary-change-button" type="button" name="CountiesBoundaries" value="Counties" />',
'<input id="boundaries-tracts" class="boundary-change-button" type="button" name="TractsBoundaries" value="Tracts" />',
'</div>'
];
// If there is no div with the id 'boundary-change-container', make one
var boundaryChange = $('div#boundary-change-container');
if(boundaryChange.length===0){
var boundaryChange = document.createElement('div');
boundaryChange.id = 'boundary-change-container';
boundaryChange.innerHTML = boundaryChangeArr.join('');
gmap.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(boundaryChange);
}; // DONE: if(boundaryChange.length===0)
google.maps.event.addListener(document.getElementById('boundaries-states'),
'click',
function(){
alert('clicked');
});
但是,當我按一下按鈕boundaries-states
,什麼也沒有發生,我甚至不看在Firebug的任何錯誤,Firefox的插件,它可以檢查JS錯誤。
我在做什麼錯?
你的HTML看起來像什麼?你可以創建一個展示問題的[jsfiddle](http://jsfiddle.net/)嗎? – geocodezip
1.你想爲DOM對象(你的按鈕)使用addDomListener。 2.您無法使用getElementById訪問按鈕,直到它們被添加到DOM。 [jsfiddle](http://jsfiddle.net/LxM4e/2/) – geocodezip