2017-01-16 85 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