要通过ng-click使用angularjs获取被点击元素的标记名

时间:2017-07-07 04:50:12

标签: html angularjs

我遇到了ng-click事件的问题。以下是我的代码。当我点击该元素时,它会检索span标记,如果我调用currentTarget,则会提供li元素。我想在<a>上获取ng-click元素。在获取元素后,我必须检查它是否有一个名为&#39; havesub&#39;的类,我该如何实现它?

<li>
  <a class="havesub" ui-sref="" ng-click="openmenu($event)">
    <img src="images/icon-03.png" alt="menu" />
    <span>Menu item1</span>
  </a>
</li>

以下是我在控制器中的功能

app.controller("con", function($scope) {
  $scope.openmenu = function($event) {
    var targets = $event.currentTarget;
  };
});

1 个答案:

答案 0 :(得分:0)

var app = angular.module("ngClickApp", []);
app.controller('ngClickCtrl', ['$scope', function ($scope) {
$scope.click=function(event){
	alert(angular.element(event.currentTarget).hasClass("havesub"));
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="ngClickApp">
<head>
	<title>Validation messages</title>
</head>
<body ng-controller="ngClickCtrl">
<form name="userForm">

 <li>
  <a href  class="havesub"  ng-click="click($event)">
    <img src="1.png" alt="menu" />
    <span>Menu item1</span>
  </a>
</li>

</form>
</body>
</html>

event.currentTarget可以正常使用

<html ng-app="ngClickApp">
<head>
    <title>Validation messages</title>
</head>
<body ng-controller="ngClickCtrl">
<form name="userForm">

 <li>
  <a href  class="havesub"  ng-click="click($event)">
    <img src="1.png" alt="menu" />
    <span>Menu item1</span>
  </a>
</li>

</form>
<script type="text/javascript">
var app = angular.module("ngClickApp", []);
app.controller('ngClickCtrl', ['$scope', function ($scope) {
$scope.click=function(event){
    alert(angular.element(event.currentTarget).hasClass("havesub"));
}
}]);
</script>
</body>
</html>