为什么我不能将动态事件处理程序附加到此元素?

时间:2012-10-23 15:05:26

标签: jquery google-maps

我的code

HTML

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js"></script>

<div id="map" style="width:500px; height:500px"></div>

<div id="mappa-infowindow" style="display:none;">
    <div style="background-color:#ffffff;">            
        <span style="cursor:pointer;" class="pulsanteProva">Click</span>
    </div>            
</div>

的jQuery /地图

$("body").on("click", ".pulsanteProva", function () {
    alert("clicked");
});

$(window).load(function () {
    var templateFinestra = $('#mappa-infowindow');
    var infoWindowOptions = {
        content: templateFinestra.html(),
        pixelOffset: new google.maps.Size(-87, -88)
    };
    var infowindow = new InfoBox(infoWindowOptions);    

    var latlng = new google.maps.LatLng(42.745334,12.738430);
    var options = { zoom: 12, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP };
    var map = new google.maps.Map(document.getElementById('map'), options);
    var marker = new google.maps.Marker({ position: latlng, map: map, title: 'Example' });   

    markerClick = function () {
        infowindow.setContent(templateFinestra.html());
        infowindow.open(map, marker);
    };    

    google.maps.event.addListener(marker, 'click', markerClick);
});

当我点击标记而不是跨度Click时,它必须向我显示警报,但实际上它不起作用。似乎处理程序没有附加?为什么呢?

不要将on放在函数中并将其附加到地图中父作品http://jsfiddle.net/arEWv/9/

1 个答案:

答案 0 :(得分:1)

似乎谷歌地图会删除渲染前附加到嵌入元素的所有事件。

查看工作jsfiddle

markerClick = function () {
        infowindow.setContent(templateFinestra.html());
        infowindow.open(map, marker);
        setTimeout(function(){$('.pulsanteProva').click(function(){alert('clicked');});},500);
    };   

这个不起作用jsfiddle

markerClick = function () {
        infowindow.setContent(templateFinestra.html());
        infowindow.open(map, marker);
       $('.pulsanteProva').click(function(){alert('clicked');});
};