本文介绍了将类添加到元素的单击事件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Angular Js 的新手.我需要在元素的点击事件中添加一个类.我尝试了以下代码.但它不起作用.
这段代码有什么问题?是否有必要使用 ng-class ?怎么做?
解决方案
可以通过$event来点击
<p ng-click='selectMe($event)'>{{data.name}}</p>//代码:$scope.selectMe = 函数(事件){$(event.target).addClass('active');}
I am new to Angular Js. I need to add a class to an element on its click event. I tried the following code. But it is not working.
<html>
<head>
<style>
.active{color:red;}
</style>
<script src="js/lib/jquery.min.js"></script>
<script src="js/lib/angular.min.js"></script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
<div ng-repeat="data in datas">
<p ng-click='selectMe()'>{{data.name}}</p>
</div>
<script>
var app = angular.module('MyApp',[]);
app.controller('MyController',function($scope){
$scope.datas = [{name:"first"},{name:"second"}];
$scope.selectMe = function (){
$(this).addClass('active');
}
});
</script>
</body>
</html>
What is the problem in this code? Is it necessary to use ng-class ? How to do it?
解决方案
You can pass $event to click
<p ng-click='selectMe($event)'>{{data.name}}</p>
//code:
$scope.selectMe = function (event){
$(event.target).addClass('active');
}
这篇关于将类添加到元素的单击事件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!