首页 > 如何像模拟hover、focus一样模拟onmouseover事件?

如何像模拟hover、focus一样模拟onmouseover事件?

一般通过这样去模拟css中的事件,那么js 中的onmouseover如何模拟?


<!DOCTYPE html>
<html>
<head>
<style type="text/css">

#div1{width: 200px;height: 200px;background-color: blue;}

</style>
</head>
<body>
<div id="div1"></div>
</body>
<script type="text/javascript">

var oDiv=document.getElementById('div1');
oDiv.onmouseover=function()
{
    oDiv.style.background='red';
}
oDiv.onmouseout=function()
{
    oDiv.style.background='blue';
}

</script>
</html>

用JS很容易实现啊。如上。


Event.createEvent()


如果你是想在 chrome 中調試時觸發事件,例如 mouseover ,可以在 console 中這麼做:

<div id="box"></div>
document.getElementById('box').onmouseover = function() {
    console.log('mouseover event trigger');
};

Chrome Devloper Tool -> Console

$("#box").onmouseover()

這裡的 $Chrome 內建選取元素的方法,不是 jQuery

【热门文章】
【热门文章】