1

プラグインjquery-tmplを使用しています。テンプレートの実行後にコールバックを指定する方法はありますか?こんなことしたい

<script id='itemTemplate' type='text/html'>
  <li class="item" id=${timestampMs}>
    <span class="content">${content}</span> 
  </li>
  ${processItem($('#' + timestampMs))}
</script>

生成されたばかりprocessItemの要素はどこで実行されますか。<li>しかし、書かれているように、その時点で存在しない要素processItemが呼び出されます。

テンプレートを実行する方法は次のとおりです。

// Make the AJAX call to the service
$.ajax({
  dataType: "json",
  url: "/getItems",
  success: function(data) {
    // fill out template from json
    $('#itemTemplate').tmpl(data).appendTo('#container');
  }
});

ありがとう!

4

2 に答える 2

4

.tmplを呼び出すと、操作できるノードがあるため、次のように実行できます。

$.ajax({
  dataType: "json",
  url: "/getItems",
  success: function(data) {
    // fill out template from json
    $('#itemTemplate').tmpl(data).each(function (index) {                           
      processItem($(this))
    }).appendTo('#container');               
  }
});

これは回避策と似ていますが、アイテムを再選択する必要はなく、呼び出しを連鎖させることができるはずです。

于 2010-10-27T22:23:17.390 に答える
0

今のところ使用している回避策は次のとおりです。

$.ajax({
  dataType: "json",
  url: "/getItems",
  success: function(data) {
    // fill out template from json
    $('#itemTemplate').tmpl(data).appendTo('#container');

    // now process the new events
    $('#container .item').each(function (index) {                           
      processItem($(this))
    });                     
  }
});
于 2010-10-27T04:20:13.670 に答える