从MouseEvent目标获取元素的类名

J. *_*nor 1 javascript typescript angular

我需要一个类的名字,EventTarget我得到这样的:

<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
}
Run Code Online (Sandbox Code Playgroud)

我希望能够将变量分配给作为目标的元素的类名.如果我使用的东西就像getSelection()我可以做的那样selection.anchorNode.parentElement.className.有没有办法在纯JavaScript中执行此操作?我正在使用Angular 2,并希望完全避免使用jQuery.

yur*_*zui 10

为了使打字稿警告静音,你可以转换EventTarget为Element然后使用className属性

(<Element>event.target).className
Run Code Online (Sandbox Code Playgroud)