博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript注册window的onload事件问题研究
阅读量:4047 次
发布时间:2019-05-25

本文共 3005 字,大约阅读时间需要 10 分钟。

在真实的编程环境中,对window.onload事件注册多个函数是很常见的事,但window.onload 不能同时加载多个函数。

 

则只有b函数生效。

 

解决方法可以是

但是代码不好看,而且还有动态参数的问题不易解决

 

下面先来看一下ms的解决方法 IE5以上的方法

注意只对IE5以上有效 方法说明参见

如果想对其它浏览器使用如FF opera等则参考如下方法

 

addEventListener原文说明如下:

 

addEventListener(

NN 6

IE n/a

DOM 2

addEventListener("eventType", listenerFunction, useCapture)

  

Binds an event handler function to the current node so that the function executes when an event of a particular type arrives at the node either as event target or during event propagation. Note that W3C DOM events propagate through text nodes, as well as element nodes. The node listens for the event type either during event capture or event bubbling propagation, depending upon the setting of the Boolean third parameter. You may invoke this method multiple times for the same node but with different parameter values to assign as many event handling behaviors as you like, but only one listener function may be invoked for the same event and propagation type. If the event listener is added on a temporary basis, it may be removed via the removeEventListener( ) method.

 
Parameters
 
  • A string of one event type (without the "on" prefix) known to the browser's object model. The W3C DOM knows the following event types (and Netscape 6 implements most of them):
  • A reference to the function to execute when the node hears the event type in the specified propagation mode. As this is a reference to a function object, do not surround the name in quotes, nor include the parentheses of the function. At execution time, the browser automatically passes the current event object as a parameter to the listener function.
  • A Boolean value. If true, the node listens for the event type only while the event propagates toward the target node (in event capture node). If false, the node listens only when the event bubbles outward from the event target. If the current node is the target of the event, either Boolean value may be used.

 

 

ms给提供了一个好的方法可以兼容两种方法,很难得的是这种方法竟然是不用微软的命名,呵呵,真稀罕

看看微软是怎么让步的吧

How to Translate attachEvent to addEventListener    

The W3C DOM Level 2 Event Model specifies addEventListener as the standard way to register event handlers. For cross-browser compatibility, the following script defines addEventListener to the window object.

 

 

在页面引导时注意让语句先运行,然后就可以用方便的方法来使用了 注意上面的useCapture一般用false值即可

 

应用代码例

 

现在注册的事件都可以执行了~

 

~the end~

转载地址:http://qefci.baihongyu.com/

你可能感兴趣的文章
当前主要目标和工作
查看>>
使用 Springboot 对 Kettle 进行调度开发
查看>>
一文看清HBase的使用场景
查看>>
解析zookeeper的工作流程
查看>>
搞定Java面试中的数据结构问题
查看>>
慢慢欣赏linux make uImage流程
查看>>
linux内核学习(7)脱胎换骨解压缩的内核
查看>>
以太网基础知识
查看>>
慢慢欣赏linux 内核模块引用
查看>>
kprobe学习
查看>>
慢慢欣赏linux phy驱动初始化2
查看>>
慢慢欣赏linux CPU占用率学习
查看>>
2020年终总结
查看>>
Homebrew指令集
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>