How to test if event.target.hasClass() using angularJS and jqlite?

牧云@^-^@ 提交于 2019-12-10 13:57:13

问题


After a click pass the event to ctrl. I want to write a conditional that will return true if the element.target has the class modal-click-shield

Question:

How can I use .hasClass() with event.target using angulars' jqlite?

Problem:

Currently I'm getting a type error saying that:

$scope.exitModal = function(event){
        // Return to current page when exiting the modal, via UI.
        // After state return, should set focus on the matching link.
        var target = event.target;
        console.log(target.hasClass('modal-click-shield'));
});

Error:

 TypeError: undefined is not a function

Html:

  <div class="modal-click-shield" ng-click="exitModal($event)">
     <div ui-view="pdw"  class="product-container"></div>
  </div>

回答1:


Your element from event.target is a regular HTMLElement, not the JQlite version. You need to do this to convert it:

angular.element(event.target);



回答2:


If you want to keep using your JS DOM element plain without use jQuery or angular:

event.target.classList.contains('modal-click-shield')



回答3:


because event.target is a DOM node, not a "jQuery" object. Wrap it

var target = $(event.target);

or

angular.element(event.target);


来源:https://stackoverflow.com/questions/28057453/how-to-test-if-event-target-hasclass-using-angularjs-and-jqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!