通用JS事件写法实现代码
作者:
对于js事件的通用调用写法
<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>
<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>