[英]bind function to newly created element
Thanks for any help. 谢谢你的帮助。
I have a function. 我有一个功能。 Inside this function, I create two buttons and assign two ids.
在这个函数中,我创建了两个按钮并分配了两个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>';
later in the same function I want to do: 后来我想做同样的功能:
$('#_updateStation').bind('click', function(){ // stuff });
But it doesn't work. 但它不起作用。 The content is added as part of a popup (OpenLayers) which in return is added to the map..
内容作为弹出窗口(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);
}
);
EDIT: 编辑:
$(document).append(content);
//map.addControl(selectControl);
//selectControl.activate();
$(document).on('click', '#createWave_updateStation' ,function(){
console.log("TES");
});
$('#create_stationPopup').popup('open');
Delegate the Event to Static parent container. 将事件委托给静态父容器。 Doing so will attach the event listener after matching the child nodes..
这样做将在匹配子节点后附加事件侦听器。
$(document).on('click', '#_updateStation',function(){ // stuff }); version >= 1.7
I am delegating it to the document as I am not sure of your HTML structure.. 我将它委托给文档,因为我不确定你的HTML结构..
So better to replace the document with a Static Container..
因此最好用
Static Container..
替换文档Static Container..
Also as of jQuery version 1.7 .on()
is the preferred method over .bind()
从jQuery版本1.7开始,
.on()
是.bind()
的首选方法
If you using version less than version 1.7 , you can use .delegate
to attach the event 如果使用的版本低于1.7版,则可以使用
.delegate
附加事件
$(document).delegate('#_updateStation' ,'click',function(){ // stuff });
// version < 1.7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.