我尝试在获得焦点时将类添加到输入的父级。我可以得到父母,但我似乎无法设置类名。这是我在这个插件中尝试的内容:
http://plnkr.co/edit/eEfSeDb7i3uujjBZt21z?p=preview
<!DOCTYPE HTML>
<html id="ng-app" ng-app="app"> <!-- id="ng-app" IE<8 -->
<head>
<link rel="stylesheet" href="http://nervgh.github.io/css/animate.min.css" />
<style>
.highlight {
border: 2px solid red;
}
.blue-border
{
border:2px solid blue;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
<script>
angular.module( 'app', [])
.run( function( $rootScope, $timeout ) {
$rootScope.focusInput= function($event )
{
angular.element($event.target).parent().className += " " + 'highlight';
console.log( angular.element($event.target).parent() );
};
$rootScope.blurInput= function($event )
{
angular.element($event.target).parent().className.replace(' highlight', '');
};
});
</script>
</head>
<body>
<div class="blue-border">
<input ng-focus="focusInput($event)" ng-blur="blurInput($event)" value="Lactobacillus acidophilus"/>
</div>
</body>
</html>
注意它如何成功记录父项。但是,边框不会变红!是否还有其他一些&#34; Angulary&#34;这样做的方式?
答案 0 :(得分:9)
Angular的JQLite提供了&#39; addClass&#39;和&#39; removeClass&#39;功能:
angular.element($event.target).parent().addClass('highlight');
请参阅此处的文档:https://docs.angularjs.org/api/ng/function/angular.element