获取点击元素的索引 [英] Get the index of the click element

查看:40
本文介绍了获取点击元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取被点击元素的索引,但我一直在控制台中获取 -1 .我正在使用 Array.prototypenodelist

获取 indexOf

HTML:

<div app-click=""><div ng-repeat="json in myJson"><li>{{json}}</li>

JS:

.directive('appClick', function() {返回 {限制:'A',范围:真实,控制器:函数($scope,$element){$element.bind("点击",function(e){var index = Array.prototype.indexOf.call($element.children(), e.target);控制台日志(索引);});}}});

JSFiddle 演示

解决方案

其他选项是设置索引

<div app-click=""><div ng-repeat="json in myJson"><li data-index="{{$index}}">{{json}}</li>

并点击阅读

e.target.dataset.index

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

解决方案

Other option is to set the index

<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>

and read it in the click

e.target.dataset.index

这篇关于获取点击元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆