[英]bind function to newly created element
谢谢你的帮助。
我有一个功能。 在这个函数中,我创建了两个按钮并分配了两个id。
var content =
'<div>
'<div id="stationPopUpbtn">' +
'<button id="createWave_updateStation" onclick="updateStationContent('+feature+')">Änderungen speichern</button>'+
'<button id="createWave_deleteStation" onclick="deleteFeature('+feature+')">Station löschen</button>'+
'</div>'+
'</div>';
后来我想做同样的功能:
$('#_updateStation').bind('click', function(){ // stuff });
但它不起作用。 内容作为弹出窗口(OpenLayers)的一部分添加,然后将其添加到地图中。
var popup = new OpenLayers.Popup.FramedCloud(
feature.id+"_popup",
feature.geometry.getBounds().getCenterLonLat(),
new OpenLayers.Size(250, 100),
content,
null, // anchor
true, // closeBox exists
//closeBoxCallback
function(){
feature.style.pointRadius = 20;
map.removePopup(feature.popup);
}
);
编辑:
$(document).append(content);
//map.addControl(selectControl);
//selectControl.activate();
$(document).on('click', '#createWave_updateStation' ,function(){
console.log("TES");
});
$('#create_stationPopup').popup('open');
将事件委托给静态父容器。 这样做将在匹配子节点后附加事件侦听器。
$(document).on('click', '#_updateStation',function(){ // stuff }); version >= 1.7
我将它委托给文档,因为我不确定你的HTML结构..
因此最好用Static Container..
替换文档Static Container..
从jQuery版本1.7开始, .on()
是.bind()
的首选方法
如果使用的版本低于1.7版,则可以使用.delegate
附加事件
$(document).delegate('#_updateStation' ,'click',function(){ // stuff });
// version < 1.7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.