获取click元素的索引 [英] Get the index of the click element
本文介绍了获取click元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取clicked元素的索引,但在控制台中一直保持 -1
.我正在使用 Array.prototype
从 nodelist
I am trying to get the index of the clicked element but I keep getting -1
in the console. I am using Array.prototype
to get the indexOf
from the nodelist
HTML:
<div ng-app='app' ng-controller='mainCtrl'>
<div app-click="">
<div ng-repeat="json in myJson">
<li>{{json}}</li>
</div>
</div>
</div>
JS:
.directive('appClick', function() {
return {
restrict: 'A',
scope: true,
controller: function($scope, $element) {
$element.bind("click",function(e){
var index = Array.prototype.indexOf.call($element.children(), e.target);
console.log(index);
});
}
}
});
JSFiddle Demo
推荐答案
其他选项是设置索引
<div ng-app='app' ng-controller='mainCtrl'>
<div app-click="">
<div ng-repeat="json in myJson">
<li data-index="{{$index}}">{{json}}</li>
</div>
</div>
</div>
并点击阅读它
e.target.dataset.index
这篇关于获取click元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文