检查鼠标是否在div内而没有悬停事件

时间:2018-04-03 11:44:41

标签: javascript jquery

<div>
  <span>target1</span>
  <span>target2</span>
</div>

在上面的代码中,
鼠标已经在target2

我想在代码中知道鼠标在target2上 如何在没有悬停事件的情况下检查鼠标是否在span内?

2 个答案:

答案 0 :(得分:1)

您可以使用此代码将鼠标放在任何元素内。

您需要提供元素ID,然后将JQuery鼠标悬停侦听器添加到该元素。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <span id="target1">target1</span>
  <span id="target2">target2</span>
</div>
class db_info_test(BASE):

__tablename__ = "info_test"

id              = Column(Integer, primary_key=True)
name            = Column(String)
project         = Column(String)
manufacturer    = Column(String)

了解更多信息

https://stackoverflow.com/help/how-to-ask

答案 1 :(得分:0)

这将允许您监视鼠标是否超过target2

的javascript:

var stateMonitor = document.getElementById("state");
var mySpan = document.querySelector("span:nth-child(2)");
mySpan.addEventListener("mouseover", function() {
    stateMonitor.innerHTML = "in";
});
mySpan.addEventListener("mouseout", function() {
    stateMonitor.innerHTML = "out";
});

HTML:

<div>
  <span>target1</span>
  <span>target2</span>
  <span id="state">out</span>
</div>