如何在google map v3中将事件处理程序分配给infowindow

时间:2014-03-15 11:26:48

标签: javascript google-maps google-maps-api-3

这是我的代码:

window.onload = function(){
  var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(5.311005375262571, 100.44538021087646)
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);


        var marker = new google.maps.Marker({
          position: new google.maps.LatLng(5.311005375262571, 100.44538021087646),
          map: map
     });





  var infoWindowContent = '<div><p id="center12" >pop up hi</p></div>'



   var infowindow = new google.maps.InfoWindow({
      content: infoWindowContent
  });
      google.maps.event.addListener(marker, 'click', function() {
       infowindow.open(map,marker);

           var mapCenter = document.getElementById("center12");
           mapCenter.onclick = function(){
              alert("hi");
           }

      });



}

我希望浏览器能够在我点击文字时提醒你,但是它显然不起作用,为什么???

1 个答案:

答案 0 :(得分:1)

click - domready - infowindow事件发生时分配 google.maps.event.addListener(infowindow,'domready',function(){ google.maps.event.addDomListener(document.getElementById("center12"), 'click', function(){ alert('hi'); }); }); - 侦听器(此时将用于显示信息窗口的元素已注入文档) :

{{1}}