簡體   English   中英

如何獲取我們單擊的元素的索引

[英]How to get the index of which element we clicked on

我有一個容器列表,我想添加一個按鈕,在我們單擊終端徽標的容器上顯示一個終端。 在此處輸入圖片說明

但是我不怎么捕捉列表中容器的索引。

我的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);
  }

我假設您要捕獲的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);
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM