【发布时间】:2017-01-17 03:47:03
【问题描述】:
我有一个容器列表,我想添加一个按钮,在我们单击终端徽标的容器上显示终端。
但我不知道如何在列表中捕获容器的索引。
我的 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);
}
【问题讨论】:
-
对,错了见谅
-
使用MeteorJS: How to get clicked element获取被点击的元素,使用Get index of clicked element in collection with jQuery获取被点击元素的索引。使用
$(event.currentTarget).closest('li').index(); -
@Tushar 可以做 .closet('className') 吗?
-
是的。您可以将任何有效的选择器传递给
closest。对于类名,.closest('. liContainer') -
谢谢,你知道为什么索引从2开始吗?
标签: javascript jquery meteor