简体   繁体   English

Javascript变量函数(Twig)

[英]Javascript Variable Function (Twig)

My default.htm (to set unique id) 我的default.htm(设置唯一ID)

{ % set uid = __SELF__.id % }

<div id="tab" name="{{uid}}" class="my-tab draggable-tab ui-draggable">
    <img id="tab-avatar" alt="Virtual agent avatar" style="height:50px; width:50px" src="{{__SELF__.avatarImage}}">
    <div id="tab-minimize">
        <div id="tab-label"></div>
        <div id="tab-expand" name="{{uid}}" class="tab-expand tab-btn"></div>
        <div id="tab-close" name="{{uid}}" class="tab-close tab-btn" onclick="tabClose()"></div>
    </div>
</div>

JS JS

function tabClose(){      
var uid = document.getElementsByName("name");
    $(this).click(function() { 
        $("#tab").addClass("hidden");  //<--- how to put the uid in?
    });
}

JS JS

var tab_{{uid}} = function tabClose(){
                       $("#tab-close").click(function() {        
                           $("#tab").addClass("hidden");
                       });
                  }

Can someone show me how to use twig in JavaScript? 有人可以告诉我如何在JavaScript中使用树枝吗?

How to set unique id for each component so that when I duplicate the component it will not have any errors? 如何为每个组件设置唯一的ID,以便在我复制组件时不会出现任何错误?

You can't call server variable (twig) in client program (javascript). 您无法在客户端程序(javascript)中调用服务器变量(twig)。

You must write the twig variable in your html code for javascript catch it. 您必须在html代码中编写twig变量,以便javascript捕获它。

For example, tout can put it in data attribute. 例如,tout可以将其放在data属性中。

<div id="tab-close" data-uniqid={{uid}} >

And in your js 在你的js里

var uid = $("#tab-close").data('uniqid');

or with your update 或您的更新

var uid = $("#tab").attr('name');

UPDATE 更新

For your problem, I think You don't need Twig variable in your js, this variable is here to have uniq Id in your html code. 对于您的问题,我认为您在js中不需要Twig变量,此变量在此处用于html代码中的uniq ID。

So, try this: 因此,请尝试以下操作:

<div id="tab{{__SELF__.id}}" class="my-tab draggable-tab ui-draggable">
    <img id="tab-avatar{{__SELF__.id}}" alt="Virtual agent avatar" style="height:50px; width:50px" src="{{__SELF__.avatarImage}}">
    <div id="tab-minimize{{__SELF__.id}}">
        <div id="tab-label{{__SELF__.id}}"></div>
        <div id="tab-expand{{__SELF__.id}}" class="tab-expand tab-btn"></div>
        <div id="tab-close{{__SELF__.id}}" class="tab-close tab-btn" onclick="tabClose()"></div>
</div>

And in your js 在你的js里

 $(".tab-close").click(function() {        
     $(this).closest(".my-tab").addClass("hidden");
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM