我想知道如何通过点击其他onmouseover
来触发div
功能。我喜欢它只用JavaScript完成。这是代码:
function showBlue() {
document.getElementById('xyz').style.background = "#425dff";
}
function showRed() {
document.getElementById('xyz').style.background = "#b43700";
}

#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="showRed()"></div>
&#13;
我尝试了什么,但没有工作:
<script>
document.getElementById('abc').addEventListener("click", triggerFunction);
function triggerFunction {
document.getElementById('xyz').onmouseover();
}
</script>
答案 0 :(得分:0)
不要试图在元素上触发事件,而是直接执行函数,如下所示:
<script>
document.getElementById('abc').addEventListener('click', triggerFunction);
function triggerFunction {
showBlue();
}
</script>