javascript - 在没有悬停的情况下触发 onmouseover 功能,但单击另一个 div

标签 javascript css triggers onmouseover

我想知道如何通过点击另一个div 来触发onmouseover 功能。我喜欢只用 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>

我尝试过但没有奏效的方法:

<script>
    document.getElementById('abc').addEventListener("click", triggerFunction);

    function triggerFunction {
        document.getElementById('xyz').onmouseover();
    }
</script>

最佳答案

与其尝试在元素上触发事件,不如像这样直接执行函数:

<script>
    document.getElementById('abc').addEventListener('click', triggerFunction);

    function triggerFunction {
        showBlue();
    }
</script>

关于javascript - 在没有悬停的情况下触发 onmouseover 功能,但单击另一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29023394/

相关文章:

javascript - ngTagsInput 3.2.0 使用 PHP 插入数组

html - 如何使用自定义搜索框进行谷歌自定义搜索?

Azure 数据工厂 - 在存储事件触发器中使用 typeProperties 参数

mysql - 尝试通过触发器(插入时)插入多行会导致错误

html - 如何让div代表虚线?

mysql - 删除后保持 MySQL 值按顺序排列

javascript - 无法将我的复选框设置为 `alignSelf: ' flex-end '` or ` right : 0`

javascript - 如何将 IEnumerable<HtmlElement> 转换为常规 HtmlElement

javascript - JQuery UI 可拖放

javascript - 当特定元素具有相同的类名时,如何获取 jQuery 函数来处理它们?