javascript - 如何让 onmouseover 和 onmouseout 不作为内联函数工作

标签 javascript html

如何让 mouseOver 和 mouseout 工作而不在元素内调用它们 <div onmouseover="mouseOver()" >

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



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

最佳答案

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

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


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

关于javascript - 如何让 onmouseover 和 onmouseout 不作为内联函数工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49603063/

相关文章:

javascript - 在鼠标悬停时更改背景颜色不透明度

javascript - 部署页面时 Jquery ajax 调用不起作用

HTML/CSS 标签 : Labels taking on the properties of other labels

html - 粘性页脚覆盖正文内容的底部

Java:从 HTML 中删除 Javascript 的最佳方法

javascript - 我如何在我的情况下显示工具提示?

javascript - 将 session 参数保存在cookie中

html - 不允许 <sup> 断行

javascript - 是否可以动态更改 manifest.json 文件

javascript - 表-解析(Kendo UI 网格): Capturing the details of the model being sent from the View to Controller