通用JS事件写法实现代码
程序员文章站
2024-02-09 19:33:16
<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>