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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:21:05  来源:igfitidea点击:

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

javascriptangularjsjavascript-eventsangularjs-ng-clickjqlite

提问by Matthew Harwood

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

单击后将事件传递给 ctrl。我想写一个条件,如果element.target有类将返回真modal-click-shield

Question:

问题:

How can I use .hasClass()with event.targetusing 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.targetis 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);