2017-01-16 158 views
0

我有一个容器列表,我想添加一个按钮,在容器上显示终端,点击终端标识。 enter image description here如何获得我们点击哪个元素的索引

但我不知道如何捕捉列表中容器的索引。

我的HTML是

<li class="liContainer"> 
    <div> 
     <h3>{{nameContainer}}</h3> 
    </div> 
    <div id="idContainer"> 
     <span>ID: {{idContainer}}</span> 
    </div> 
    <div id="stateContainer"> 
     <span class="state">State: {{stateContainer}}</span> 
    </div> 

    <div id="terminalContainer" class="hidden"> 
     <div class="terminal hidden"></div> 
    </div> 

     <button type="button" class="stop {{#if to_hide_stop}}hidden{{/if}}"> </button> 
     <button type="button" class="start {{#if to_hide_start}}hidden{{/if}}"> </button> 
     <button type="button" class="pause {{#if to_hide_pause}}hidden{{/if}}"></button> 
     <button type="button" class="unpause {{#if to_hide_unpause}}hidden{{/if}}"> </button> 
     <button type="button" class="cmdLogs"> terminal </button> 
</li> 

而我的JS:

'click .cmdLogs'(event) { 
    Session.set("displayCmdLogs",true); 
    //here I need to do something to get the ID with the event and then I could do... 
    setTimeout(function(){ 
     var term = new Terminal(); 
     console.log("term: " + term); 
     //.. the getElement on the right one 
     term.open(document.getElementsByClassName('terminal')[idFromEvent]); 
     //term.fit(); 
     term.write('Hello from container.js'); 
    },200); 
    } 
+0

权,遗憾的错误 – Jerome

+1

通过获取点击的元素[MeteorJS:如何获得点击的元素(HTTP ://stackoverflow.com/questions/35194509/meteorjs-how-to-get-clicked-element)和单击元素的索引使用[获取jQuery集合中单击元素的索引](http://stackoverflow.com /问题/ 5545283/GET-指数的-点击元素,在收集与 - jQuery的)。使用'$(event.currentTarget).closest('li').index();' – Tushar

+0

@Tushar是否可以做.closet('className')? – Jerome

回答

0

我假设你想赶上id为 “idContainer”。我会修改你的HTML如下:

<li class="liContainer"> 
    <div> 
     <h3>{{nameContainer}}</h3> 
    </div> 
    <div id="idContainer"> 
     <span>ID: {{idContainer}}</span> 
    </div> 
    <div id="stateContainer"> 
     <span class="state">State: {{stateContainer}}</span> 
    </div> 

    <div id="terminalContainer" class="hidden"> 
     <div class="terminal hidden"></div> 
    </div> 

     <button type="button" class="stop {{#if to_hide_stop}}hidden{{/if}}"> </button> 
     <button type="button" class="start {{#if to_hide_start}}hidden{{/if}}"> </button> 
     <button type="button" class="pause {{#if to_hide_pause}}hidden{{/if}}"></button> 
     <button type="button" class="unpause {{#if to_hide_unpause}}hidden{{/if}}"> </button> 
     <button type="button" class="cmdLogs" id="{{idContainer}}"> terminal </button> 
</li> 

你JS:

'click .cmdLogs'(event, template) { 
    Session.set("displayCmdLogs",true); 
    var id = event.currentTarget.id; //The id is here 

    setTimeout(function(){ 
     var term = new Terminal(); 
     console.log("term: " + term); 
     //.. the getElement on the right one 
     term.open(document.getElementsByClassName('terminal')[idFromEvent]); 
     //term.fit(); 
     term.write('Hello from container.js'); 
    },200); 
    } 
+0

不,我想赶上“liContainer”的ID来知道哪个容器,我点击,但现在它的作品! – Jerome