问题
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