如何在 JavaScript 中的元素上触发 Onmouseover 事件
how to trigger an onmouseover event on element in javascript?
如标题所示,如何在 javascript 中的元素上触发 onmouseover
事件?请参考代码片段。我可以单击小黑框并执行onmouseover
事件,而不是将鼠标悬停在大框上吗?目标不是将盒子变成蓝色,而是触发onmouseover
事件。而且我需要只用JavaScript来完成,请不要使用Jquery。
function showBlue() {
document.getElementById('xyz').style.background = "#425dff";
}
function showRed() {
document.getElementById('xyz').style.background = "#e2e2e2";
}
#abc{width: 50px; height: 50px; background: #000000; color:#ffffff; cursor: pointer;}
#xyz{width: 200px; height: 200px; background: #e2e2e2;}
<div id="abc"><a>click me</a></div>
</br>
<div id="xyz" onmouseover="showBlue()" onmouseout="showGrey()"></div>
我试过这个,但对我不起作用:(
<script>
document.getElementById('abc').addEventListener("click", triggerFunction);
function triggerFunction() {
document.getElementById('xyz').mouseover();
}
</script>
这应该有效:
function showBlue() {
document.getElementById('xyz').style.background = "#425dff";
}
function showGrey() {
document.getElementById('xyz').style.background = "#e2e2e2";
}
function triggerMouseOver() {
document.getElementById('xyz').onmouseover();
}
#abc{width: 50px; height: 50px; background: #000000; color:#ffffff; cursor: pointer;}
#xyz{width: 200px; height: 200px; background: #e2e2e2;}
<div id="abc" onclick="triggerMouseOver()"><a>click me</a></div>
</br>
<div id="xyz" onmouseover="showBlue()" onmouseout="showGrey()"></div>
您也可以简单地这样做:
<div id="abc" onclick="document.getElementById('xyz').onmouseover()"><a>click me</a></div>
它应该是onmouseover()
而不是mouseover()
document.getElementById('xyz').onmouseover();
相关文章:
- onmouseover事件行为不端
- 在onmouseover事件中更改图像,并为每个事件设置中断/间隔
- 结合onmouseover事件和<a>标签
- Javascript:在每个onmouseover事件之后执行2个函数
- 如何在 JavaScript 中的元素上触发 Onmouseover 事件
- GraphDracula:如何检测onclick和onmouseover事件
- onmouseover事件仅在第二次鼠标悬停后才起作用
- onmouseover事件不起作用
- 如何通过onmouseover事件停止jquery旋转木马
- 从一系列onMouseOver事件中创建一个循环
- 使用JavaScript,为什么我的onmouseover事件没有被触发
- 在Firefox中的onmouseover事件
- 如何添加onmouseover事件javascript
- 为什么我的JavaScript onmouseover事件不火
- jQuery防止onmouseover事件
- Div隐藏链接上的onmouseover事件
- 将onmouseover事件分配给动态生成列表
- 将参数传递给onmouseover事件
- 在类中使用javascript的onmouseover事件的问题
- 在onmouseover事件中播放声音效果