2011-02-15 72 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这将做你想要的。 “

”将事件处理程序附加到与当前选择器匹配的所有元素,现在和将来。“

相关问题