如何在使用AngularJS将鼠标悬停在另一个div上时更改一个div上的类?

前端之家收集整理的这篇文章主要介绍了如何在使用AngularJS将鼠标悬停在另一个div上时更改一个div上的类?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在使用AngularJS指令将鼠标悬停在另一个div上时更改一个div的类.这是我到目前为止 http://jsfiddle.net/E8nM5/38/

HMTL

  1. <div ng-controller="Ctrl" ng-app>
  2. <div ng-class="my-class">This div will change class when one hovers over bottom DIV </div>
  3. <br/>
  4. <div class="hover-div" ng-mouseenter="my-class = 'highlight'" ng-mouseleave="my-class = 'lowlight'">HOVER OVER ME TO CHANGE THE UPPER DIV's CLASS</div>
  5. </div>

CSS

  1. div.highlight {
  2. padding: 10px;
  3. background: red;
  4. color: white;
  5. }
  6.  
  7. div.lowlight {
  8. padding: 10px;
  9. background: blue;
  10. color: white;
  11. }
  12.  
  13. div.hover-div {
  14. padding: 10px;
  15. background: green;
  16. color: white;
  17. }

JS

  1. function Ctrl($scope){
  2. }

有任何想法吗?

将my-class更改为myclass(即破折号导致问题).
  1. <div ng-controller="Ctrl" ng-app>
  2. <div ng-class="myclass">This div will change class when one hovers over bottom DIV </div>
  3. <br/>
  4. <div class="hover-div" ng-mouseenter="myclass = 'highlight'" ng-mouseleave="myclass = 'lowlight'">HOVER OVER ME TO CHANGE THE UPPER DIV's CLASS</div>
  5. </div>

更新:表达式中不允许my-class的原因是因为AngularJS将短划线视为减号并试图以这种方式解析它.显然,它无法解析语句my – class =’highlight’.不幸的是,在阅读AngularJS解析器代码后,我找不到一种方法来“帮助”它区分破折号和减号.

猜你在找的Angularjs相关文章