代码之家  ›  专栏  ›  技术社区  ›  yayitswei

jquery temp的回调函数?

  •  1
  • yayitswei  · 技术社区  · 15 年前

    jquery-tmpl . 是否可以在运行模板后指定回调?我想做点什么

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

    processItem <li> 刚刚生成的元素。不过,正如它所写的,元素当时并不存在 处理项目 被称为。

    // 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');
      }
    });
    

    谢谢!

    2 回复  |  直到 15 年前
        1
  •  4
  •   jrduncans    15 年前

    在调用.tmpl之后,可以对节点执行操作,因此可以执行以下操作:

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

    这类似于你的解决方法,但你不需要重新选择项目,你应该能够链呼叫。

        2
  •  0
  •   yayitswei    15 年前

    以下是我目前使用的解决方法:

    $.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))
        });                     
      }
    });