1

に をアタッチしJQuery UI SelectableましたがUL elementli itemその中の は動的に作成されていました。もちろん、それらを選択することはできませんでしたplugin's function to dynamic created elements in JQuery

例えば:

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

</ul>

[アップデート]

JQuery コード:

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

そして、私が使用する場所delegateまたはlive!?

delegateおよび使用法は、liveこの方法でイベントをバインドする方法です。

$('#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!?

どうもありがとうございました!!

4

2 に答える 2

1

jQuery は.liveを提供しており、これはまさにあなたが望むことを行います。

「現在および将来において、現在のセレクターに一致するすべての要素のイベントにハンドラーをアタッチします。」

于 2011-02-15T03:42:18.240 に答える
0

使用.live()または.delegate()


さて、更新のために、DOMに要素$( "#selectable" ).selectable();をアタッチした直後に呼び出します。#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');トリガーされる唯一のアラートになります。

于 2011-02-15T03:43:16.977 に答える