【发布时间】:2020-07-14 04:22:00
【问题描述】:
我需要检测我是否仅使用 vanilla javascript 悬停一个元素。我正在尝试这个:
this.element.addEventListener('mousemove', function () {
var result = this.element.matches(':hover');
if ( result ) {
this.element.style.backgroundColor = "yellow";
}
console.log(result)
}.bind( this ))
但它没有按预期工作,因为它总是控制台记录“假”。
用例是:一个元素有一个类,如果我将其悬停,该类将被移除并添加另一个类。
我知道 jQuery 有它的“悬停”功能,但我只需要使用 vanilla javascript 来完成它。
【问题讨论】:
标签: javascript