5

有谁知道如何将用于呈现 jQuery 模板的数据项附加到生成的 dom 对象?

给定这个tmpl:

<script id="sectionTemplate" type="text/x-jquery-tmpl">
<div class="story-writer-section sticker ${CssClasses}">
    <div class="title">
    <div class="delete-button sticker-button"/>
        ${SectionName}
    </div>
    <div class="story-writer-story-container">
    </div>
</div>

还有这个 :

    $("#sectionTemplate")
        .tmpl(sections)
        .appendTo(".story-writer-section-container");

我想通过 .data('section', data) 将每个部分附加到结果中,以便稍后我可以访问所有数据,因为并非所有数据都被渲染,例如我需要掌握的 id 等稍后在执行删除之类的操作时。

也许像这样的东西......

        $("#sectionTemplate")
        .tmpl(sections)
        .appendTo(".story-writer-section-container").each(function (????) { 
                               this.data('section', ?????); 
                          });

但我不知道如何获取用于呈现此 tmpl 的当前数据项。

Perhpas 有办法将它包含在模板本身中吗?

非常感谢任何帮助。

4

1 回答 1

14

好的,我已经解决了。看起来数据已经添加到生成的元素中.tmpl()并且可以像这样访问:

$.tmplItem(this).data

非常好,像往常一样,jQuery 不会让人失望。

于 2010-12-03T13:03:04.110 回答