2017-07-13 46 views
0

我正試圖從當前tr找到具有特定類的下一個tr元素。如果下一個tr沒有那個類(打開),我想要打開下一個tr類。如何做到這一點的角度JS在角度js中找到具有特定類的下一個元素

模板

<tr ng-repeat-start = "item in xx" ng-class="{'opened': item.ddd == 0 || item.ddd == undefined,'closed': item.ddd == 1}"> 

爲TD ggff一些代碼

JS

app.directive("moveNext", function() { 
return { 
    restrict: "A", 
    link: function($scope, element) { 

     element.on('td, keyup' ,function(e) { 
      var nextele = element.parents('tr').next('.opened'); 

     }); 
    } 
} 
}); 
+0

嘗試使用(jqlite https://docs.angularjs.org/api/ng/function/angular.element)children()。並返回到DOM的JavaScript獲取類和分區它 –

+0

我想下一個開放類的子句 –

+0

然後使用.next(),甚至是兒童 –

回答

0

最後我有一點點的時間來讓你一個fddle,期待我所有的理解你:

<div ng-controller="MyCtrl"> 
    <h2>DEMO</h2> 
    <table> 
     <tr ng-click="getAll($event.currentTarget)" ng-repeat = "item in xx" ng-class="{'opened': item.ddd == 0 || item.ddd == undefined,'closed': item.ddd == 1}"> 
     <td>{{item.name}}</td> 
     </tr> 
     </table> 
</div> 
var myModule = angular.module('myModule', []) 

    .controller('MyCtrl', ['$scope', function ($scope) { 
      $scope.getAll=function(e){ 
      var elms = angular.element(e).parent().find('tr'); 
      var elm = null; 
      for(var i =0; i<elms.length; i++){ 
      console.log(/opened/.test(angular.element(elms[i]).attr('class'))); 
      if(/opened/.test(angular.element(elms[i]).attr('class'))){ 
       elm = angular.element(elms[i]); 
       break; 
      } 
      } 
      console.log('all', angular.element(e).parent().find('tr'), 'only the nex at clicked', elm); 
     }; 
     $scope.xx =[{ 
      name: 'uno', 
      ddd:1 
     },{ 
      name:'open-1', 
      ddd:0 
     },{ 
      name:'tres', 
      ddd:1 
     },{ 
     name: 'open-2', 
     ddd:0 
     }]; 
    }]); 
相關問題