Get Class Name of Element from MouseEvent Target

J. Adam Connor

I need the class name of an EventTarget, which I'm getting like this:

<div class="orig-post" (mousemove)="onMouseMove($event)"></div>

onMouseMove(event: MouseEvent): void {
    this.target = event.target
    console.log(this.target) // Outputs the element the mouse is currently over
}

I'd like to be able to assign a variable to the class name of the element that is the target. If I were using something like getSelection() I could just do something like selection.anchorNode.parentElement.className. Is there a way to do this in pure javascript? I'm using Angular 2 and would like to avoid jQuery entirely.

yurzui

In order to silence the typescript warning you can cast EventTarget to Element and then use className property

(event.target as Element).className

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Get The Class Name From an Object Variable

分類Dev

Shortest way to get last element by class name in javascript

分類Dev

Vanilla JS: How to target element by type and class

分類Dev

Angular 6 + get elements with class name from document and change style

分類Dev

Jquery Get a dynamic class name from a dynamic field

分類Dev

How to get the child of a element with event.target

分類Dev

React - get value from target

分類Dev

speed of declaring element with class name vs. class name

分類Dev

XmlSerializer - remove class name as parent element from xml output using c#

分類Dev

Access a class from a different target with marked as "public"

分類Dev

Get ID via class name

分類Dev

JavaScript get Elements By Class Name

分類Dev

Python + Selenium - Cannot locate element by class name

分類Dev

asking for Selenium *find_element_by_class_name

分類Dev

jQuery onclick to change class name of an element

分類Dev

How do I get the name from object and assign it to a new created element?

分類Dev

Get distance and angle from OrbitControl's target

分類Dev

Vue.js + Element UI: Get "event.target" at change

分類Dev

how to get value of specifice tag element from user defined class in jquery

分類Dev

How to search an Event to get the name of an Element?

分類Dev

Python tinydb - How to get specific element name?

分類Dev

How can I extract values as strings from an xml file based on the element/property name in a generated .Net class or the original XSD?

分類Dev

GraphServiceClient get group from name

分類Dev

Get column name from JSON

分類Dev

Swift: Get an element from a tuple

分類Dev

How to find a value from a specific element by name?

分類Dev

タイプ 'MouseEvent <Element、MouseEvent>'はタイプ 'MouseEvent <HTMLDivElement、MouseEvent>'に割り当てることができません

分類Dev

How to get the class name of the data model in Angular?

分類Dev

How to get caller function class name

Related 関連記事

  1. 1

    Get The Class Name From an Object Variable

  2. 2

    Shortest way to get last element by class name in javascript

  3. 3

    Vanilla JS: How to target element by type and class

  4. 4

    Angular 6 + get elements with class name from document and change style

  5. 5

    Jquery Get a dynamic class name from a dynamic field

  6. 6

    How to get the child of a element with event.target

  7. 7

    React - get value from target

  8. 8

    speed of declaring element with class name vs. class name

  9. 9

    XmlSerializer - remove class name as parent element from xml output using c#

  10. 10

    Access a class from a different target with marked as "public"

  11. 11

    Get ID via class name

  12. 12

    JavaScript get Elements By Class Name

  13. 13

    Python + Selenium - Cannot locate element by class name

  14. 14

    asking for Selenium *find_element_by_class_name

  15. 15

    jQuery onclick to change class name of an element

  16. 16

    How do I get the name from object and assign it to a new created element?

  17. 17

    Get distance and angle from OrbitControl's target

  18. 18

    Vue.js + Element UI: Get "event.target" at change

  19. 19

    how to get value of specifice tag element from user defined class in jquery

  20. 20

    How to search an Event to get the name of an Element?

  21. 21

    Python tinydb - How to get specific element name?

  22. 22

    How can I extract values as strings from an xml file based on the element/property name in a generated .Net class or the original XSD?

  23. 23

    GraphServiceClient get group from name

  24. 24

    Get column name from JSON

  25. 25

    Swift: Get an element from a tuple

  26. 26

    How to find a value from a specific element by name?

  27. 27

    タイプ 'MouseEvent <Element、MouseEvent>'はタイプ 'MouseEvent <HTMLDivElement、MouseEvent>'に割り当てることができません

  28. 28

    How to get the class name of the data model in Angular?

  29. 29

    How to get caller function class name

ホットタグ

アーカイブ