<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
function aaa()
{
alert('a');
}
function bbb()
{
alert('b');
}
//1.谁
//2.事件
//3.函数
function myAddEvent(obj, sEvent, fn)//封装成函数,
{
if(obj.attachEvent)//IE下使用attachEvent
{
obj.attachEvent('on'+sEvent, fn);
}
else//FF下使用addEventListener
{
obj.addEventListener(sEvent, fn, false);
}
}
window.onload=function ()
{
var oBtn=document.getElementById('btn1');
myAddEvent(oBtn, 'click', aaa);
myAddEvent(oBtn, 'click', bbb);
};
</script>
</head>
<body>
<input id="btn1" type="button" value="aaa" />
</body>
</html>
何时要用上事件绑定?
在同一个事件上加两个函数时,需要使用事件绑定,以完成不同的功能。