【DOM编程艺术】window.onload的工作机制

<!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=gb2312" />
<title>window.onload研究</title>
</head>

<body>
<script type="text/javascript">
window.onload=firstFunction;
window.onload=secondFunction;
function firstFunction(){
   alert('我是第一个函数');
}
function secondFunction(){
   alert('我是第二个函数');
}
</script>
</body>
</html>

结果会弹出窗口      我是第二个函数!!!!

*****  他们当中只有最后那个才会被执行

 

要想两个都执行,可以先创建一个匿名函数来容纳这两个函数,然后把这个匿名函数绑定到onload事件上,如下所示:

<script type="text/javascript">
window.onload=function(){     
    firstFunction();
    secondFunction();
}
function firstFunction(){
   alert('我是第一个函数');
}
function secondFunction(){
   alert('我是第二个函数');
}
</script>

它确实能很好的工作-----在需要绑定的函数不是很多的场合,这应该是最简单的解决方案了

转载于:https://www.cnblogs.com/positive/p/3662424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值