简体   繁体   中英

How to assign event handler to infowindow in google map v3

This is my code:

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");
           }

      });



}

I expect the browser to alert hi when I clicked on the text 'pop up hi', but it obviously doesn't work, why???

Assign the click -listener when the domready -event of the infowindow fires(at this time the elements that will be used to display the infowindow have been injected into the document):

  google.maps.event.addListener(infowindow,'domready',function(){
     google.maps.event.addDomListener(document.getElementById("center12"),
                                      'click',
                                       function(){
                                            alert('hi');
                                        });
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM