指令中的DOM元素

时间:2015-10-08 11:48:19

标签: javascript angularjs directive

我有三个小组,想知道哪个小组是关于我怎么知道的?

我想问问哪个元素是正确的,然后我想添加一个类。

myApp.directive('myDir', function ($rootScope, $interval) {
return {
    restrict: 'A',
    scope: {
    }
    link: function (scope, element, attrs) {
        $interval(function () {
            var data = element[0].getBoundingClientRect();

            for (var i = 0; i < $rootScope.gazePoints.length; i++) {
                var x = $rootScope.gazePoints[i].x;
                var y = $rootScope.gazePoints[i].y;
                if (x >= data.left && x <= data.right && y >= data.top && y < data.bottom) {  

                    var look = element.children('panel1');
                    if (look == element) // is not working
                    {
                        console.log("found")
                        element.addClass('onFocus');
                    }             
                }
            }

          }, 3000);
        }
     };
  });

也无法正常工作:document.getElementById('panel1');

我的HTML:

<div class="container" my-dir></div>
        <div class="panel panel-primary" id='panel1'>
               <div class="panel-body">
                        Panel content1
               </div>
         </div>
         <div class="panel panel-primary" id='panel2'>
               <div class="panel-body">
                        Panel content2
               </div>
         </div>
         <div class="panel panel-primary" id='panel3'>
               <div class="panel-body">
                        Panel content3
               </div>
         </div>
 </div> 

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为内在的功能有点刺激。现在您可以看到我只想知道哪个元素是当前元素。

myApp.directive('myDir', function ($rootScope, $interval) {
return {
restrict: 'A',
scope: {
}
link: function (scope, element, attrs) {
    $interval(function () {

       var look = element; // the current but how do I know which panel???


      }, 3000);
    }
 };

});