简体   繁体   English

onMouseEnter 事件不起作用。 当鼠标指针滚动到 div 主体时

[英]onMouseEnter event is not working. When mouse pointer is scrolled into the div body

Try this is JavaScript, create div and add a onmouseenter event on that div, when you move the mouse cursor on to the div, this event will be called as expected.试试这个是 JavaScript,创建 div 并在该 div 上添加一个 onmouseenter 事件,当您将鼠标 cursor 移动到该 div 上时,该事件将按预期调用。 but this event is only called when you move the cursor onto the div body not when you scroll into the div body, scroll down into the page and place the mouse cursor where the div is suppose to be, Now without moving the cursor scroll back up and the cursor will eventually end up on the div body but in this case onmouseenter event is not called.但是仅当您将 cursor 移动到 div 主体上时才会调用此事件,而不是当您滚动到 div 主体时,向下滚动到页面并将鼠标 cursor 放置在 div 应该是的位置,现在无需移动 Z179027607A849037309 向上滚动并且 cursor 最终会出现在 div 主体上,但在这种情况下不会调用 onmouseenter 事件。

  document.querySelector('div').onmouseenter = () => {
    console.log('hereInDivBody')
  }

How can I fix this?我怎样才能解决这个问题?

First the event is called mouseenter or mouseover but not onmouseenter (which exist as an attribute).首先,该事件称为mouseentermouseover ,但不是onmouseenter (作为属性存在)。 In JS you need to add an `eventListener) to listen for that event in the first place:在 JS 中,您首先需要添加一个 `eventListener) 来监听该事件:

 document.querySelector('div').addEventListener('mouseenter', function() { console.log('hereInDivBody'); })
 div { height: 50vh; border: 2px dashed red; }
 <div></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM