dplbf4340 2013-09-27 07:36
浏览 36
已采纳

Smarty:不能两次使用相同的模板

I'm having the following Smary template (it's a file-tree, files are populated separately and then list_html is assigned to geberated file tree hrml)

<script>
    $("#directory-list-root").on("click", "div.content", function() {
        $("div.selected", "#directory-list-root").removeClass("selected");
        $(this).addClass("selected");

        // Check if directory is expanded
        var node = $(this).parent("li.directory");
        if (node.hasClass("expanded")) {
            node.removeClass("expanded");
            $(this).siblings("ul.container").hide();
        } else {
            node.addClass("expanded");
            $(this).siblings("ul.container").show();
        }
    } );
</script>

<div id="directory-list-root">
    <ul class="container" style="padding:5px" id="tree">
{$list_html}
    </ul>
</div>

In the code it's used twice (in jquery-ui dialogs that are shown one after another). And the problem is that for the first time handling function is OK, but for the second click-handler is not working. If I remove tree from the first dialog then handlers in the second are assigned.

Do you know how that can be fixed?

Thank you in advance!

PS. I'm running Smarty 3.1.13 if that matters.

  • 写回答

1条回答 默认 最新

  • duan246558 2013-09-27 08:09
    关注

    If the template is used twice there will be two elements with the id directory-list-root in the document, which is illegal. The result is that things won't work.

    Instead of using a fixed id, use a dynamic one by incrementing an internal counter variable each time, e.g. by using {counter}:

    <script>
        $(function() {
            {counter name=tree assign=uniqueId}
            var $root = $("#directory-list-root-{$uniqueId}");
    
            $root.on("click", /* etc */);
        });
    </script>
    
    <div id="directory-list-root-{$uniqueId}">
        <ul class="container" style="padding:5px" id="tree">
    {$list_html}
        </ul>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?