由于各种原因,我无法在这部分项目中使用jQuery。所以我需要在 Vanilla js 中检测到这一点。我试过这个但它不起作用:
var myDiv = document.getElementById('foo');
myDiv.onmouseenter = function() {
alert('entered');
}
myDiv.onmouseleave = function() {
alert('left');
}
最佳答案
myDiv.onmouseover = function(event) {
if (this != event.currentTarget) { return false; }
// mouse enter ...
}
myDiv.onmouseout = function(event) {
if (this != event.currentTarget) { return false; }
// mouse leave ...
}
关于javascript - 如何在纯 JavaScript 中检测 mousein 和 mouseleave?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15280531/