簡體   English   中英

將函數綁定到新創建的元素

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM