当前位置:主页 > 网页前端 > JavaScript代码 >

通用JS事件写法实现代码

时间:2021-08-20 10:03:56 | 栏目:JavaScript代码 | 点击:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>test</title>
<script language="javascript" type="text/javascript">
window.onload = function()
{
var oImg = document.getElementById("myImg");
oImg.onclick = handle;
oImg.onmouseover = handle;
}
function handle(oEvent)
{
var oDiv = document.getElementById("display");

//处理浏览器兼容性,获得事件对象
if(window.event) //判断是否为IE浏览器
{
oEvent = window.event;
}

if(oEvent.type == "click")
{
oDiv.innerHTML += "点击<br>";
}
if(oEvent.type == "mouseover")
{
oDiv.innerHTML += "移动<br>";
}
}
</script>
</head>
<body>
<img id="myImg" src="test.gif" />
<div id="display"></div>
</body>
</html>

您可能感兴趣的文章:

相关文章