Home > Web Front-end > JS Tutorial > JavaScript Event learning addendum addEventSimple_javascript skills

JavaScript Event learning addendum addEventSimple_javascript skills

WBOY
Release: 2016-05-16 18:34:52
Original
886 people have browsed it
Copy code The code is as follows:

function addEventSimple(obj,evt,fn) {
if ( obj.addEventListener)
obj.addEventListener(evt,fn,false);
else if (obj.attachEvent)
obj.attachEvent('on' evt,fn);
}

removefunctionSimple(obj,evt,fn) {
if (obj.removeEventListener)
obj.removeEventListener(evt,fn,false);
else if (obj.detachEvent)
obj .detachEvent('on' evt,fn);
}

Usage is as follows:

Copy code The code is as follows:

addEventSimple(element,'click',functionname);
addEventSimple(document,'load',functionname2);

Now if the element element is clicked, functionaname will be triggered. The same document also has onload event handler functionname2.

Defect: The this keyword in IE does not point to the element that triggers the event as you imagine, but points to the entire document.

Original address: http://www.quirksmode.org/js/eventSimple.html

I went back to my hometown for the New Year, so I can’t publish it in time. I will continue next year. Happy New Year everyone!

Please keep the following information for reprinting
Author: Beiyu (tw:@rehawk)
Article source: beiyu.cnblogs.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template