为什么我不能将动态事件处理程序附加到此元素? [英] Why I can't attach an dynamic event handler to this element?
本文介绍了为什么我不能将动态事件处理程序附加到此元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码:
<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/Maps
$("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
时,它必须向我显示警报,但实际上它不起作用.似乎未附加处理程序?为什么?
when I click on the marker, than on the span Click
, it must show to me the alert, but in fact it doesnt works. Seems that the handler is not attached? Why?
将on
放在函数内部并从地图父目录附加它都不会起作用 http://jsfiddle. net/arEWv/9/
Neither putting the on
inside the function and attach it from the map parent works http://jsfiddle.net/arEWv/9/
推荐答案
类似Google Map的对象在渲染之前会删除所有附加到嵌入式元素的事件.
Seems like google map remove all events attached to embedded elements before rendering.
看到有效的 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');});
};
这篇关于为什么我不能将动态事件处理程序附加到此元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文