如何使onmouseover和onmouseout不作为内联函数工作

时间:2018-04-01 22:06:05

标签: javascript html

如何让mouseOver和mouseout无法在元素<div onmouseover="mouseOver()" >

中调用它们

&#13;
&#13;
document.getElementById('smallBox').addEventListener("onmouseover", function() {
  document.getElementById('smallBox').style.color = "blue;"
});



document.getElementById('smallBox').addEventListener("onmouseout", function() {
  document.getElementById('smallBox').style.color = "yellow;"
});
&#13;
#smallBox {
  background-color: green;
  width: 100px;
  height: 100px;
}
&#13;
<div id="smallBox">hi</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

事件的名称实际上是mouseovermouseout。我必须改变的另一件事是颜色属性,它应该是"blue"而不是"blue;"

&#13;
&#13;
document.getElementById('smallBox').addEventListener("mouseover", function() {
  document.getElementById('smallBox').style.color = "blue";
});


document.getElementById('smallBox').addEventListener("mouseout", function() {
  document.getElementById('smallBox').style.color = "yellow";
});
&#13;
#smallBox {
  background-color: green;
  width: 100px;
  height: 100px;
}
&#13;
<div id="smallBox">hi</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

在onload函数下为JS中的该元素添加事件处理程序。 document.getElementById("smallbox").addEventListener("mouseover", myFunction)

在加载DOM时需要调用此函数。      document.getElementById("smallbox").addEventListener("load", functionName)

Nd用于活动参考,请访问w3Schools