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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)