如何用JS写鼠标触发事件

2025-03-05 02:23:05
推荐回答(1个)
回答1:

简单的示例   div的背景色 会随着鼠标进入变红 离开变白 点击 变绿  双击变蓝

var divs = document.getElementsById('div1');
    divs.onmouseover = function(){
        this.style.background = 'red'
    }
    divs.onmouseout = function(){
        this.style.background = ''
    }
    divs.onclick = function(){
        this.style.background = 'green'
    }divs.ondblclick = function(){
       this.style.background = 'blue'
   }

代码要写在  window.onload ()  或者  document.onready()