javascript 如何使用angularJS和jqlite测试event.target.hasClass()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28057453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test if event.target.hasClass() using angularJS and jqlite?
提问by Matthew Harwood
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
单击后将事件传递给 ctrl。我想写一个条件,如果element.target
有类将返回真modal-click-shield
Question:
问题:
How can I use .hasClass()
with event.target
using angulars' jqlite
?
如何使用.hasClass()
与event.target
利用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>
回答by risto
Your element from event.target
is a regular HTMLElement, not the JQlite version. You need to do this to convert it:
您的元素来自event.target
常规 HTMLElement,而不是 JQlite 版本。你需要这样做来转换它:
angular.element(event.target);
回答by iarroyo
If you want to keep using your JS DOM element plain without use jQuery or angular:
如果你想继续使用你的 JS DOM 元素而不使用 jQuery 或 angular:
event.target.classList.contains('modal-click-shield')
回答by epascarello
because event.target is a DOM node, not a "jQuery" object. Wrap it
因为 event.target 是一个 DOM 节点,而不是一个“jQuery”对象。包起来
var target = $(event.target);
or
或者
angular.element(event.target);