2011-02-15 122 views
1

我是附JQuery UI SelectableUL element。但li item這裏面是動態創建當然。而且,我不能讓them.So的選擇,如何安裝plugin's function to dynamic created elements in JQuery!?如何將插件的功能附加到JQuery中的動態創建元素?

E.g:

<ul id="selectable"> 
    <li class="dataItem">item dynamic created here but you can't select me</li> 

</ul> 

[更新]

jQuery代碼:

$("#selectable").selectable(); 

,並在那裏我用delegatelive!?

delegatelive用法是將事件綁定這樣的方式:

$('#selectable').delegate('li','click',function(){ 
    // do something here 
}); 

selectable plugin's events是:

Supply a callback function to handle the selected event as an init option. 

$(".selectable").selectable({ 
    selected: function(event, ui) { ... } 
}); 

和新添加的項目沒有拿到selectable plugin's狀態如:

ui-selectee 

So,should I re-attach the plugin at every time when a new item added!? 

謝謝你非常感謝!

+0

已更新的答案... – Reigel 2011-02-15 04:03:55

回答

0

使用.live().delegate()


好,你更新,叫$("#selectable").selectable();你在DOM附加#selectable元素的時間之後。

例如,

$('#selectable').click(function(){ 
    alert('I will not fire'); 
}); 

$('body').append('<ul id="selectable"><li class="dataItem">item</li></ul>'); 

$('#selectable').click(function(){ 
    alert('I will fire'); 
}); 

alert('I will fire');將是唯一的警告觸發的。

1

jQuery提供.live這將做你想要的。 「

」將事件處理程序附加到與當前選擇器匹配的所有元素,現在和將來。「

相關問題