JS Cross-browser event

Source: Internet
Author: User


<!doctype html>
<meta charset= "Utf-8"/>
<meta content= "ie=8" http-equiv= "x-ua-compatible"/>
<title>event System by Masaki </title>
<style type= "Text/css Tutorial" >
#target {
width:400px;
height:100px;
Background:blue;
}
</style>
<script type= "Text/javascript Tutorial" >
var dom = {};
Array.prototype.indexOf = function (el, index) {
var n = this.length>>>0,
i = index = = null? 0:index < 0? Math.max (0, n + index): index;
for (; i < n; i++)
if (i in this && this[i] = = EL) return i;
return-1;
};
http://msdn.microsoft.com/zh-cn/library/bb383786.asp Tutorial X
Removes the first occurrence of an element in an Array object.
array.prototype.remove= function (item) {
var index = This.indexof (item);
if (index!==-1) return this.removeat (index);
return null;
};
Removes the element at the specified position in the Array object.
array.prototype.removeat= function (index) {
Return This.splice (index, 1)
};
Dom.attachevent = function (el, type, handler) {
Set an object-type private definition attribute on each element events
if (!el.events) el.events = {};
This object has many keys for the event type, and the value is the property of the function array
var handlers = El.events[type];
if (!handlers) {
Handlers = El.events[type] = [];
If it was originally bound to an event in a onxxxx way, then place it as the first element of the event array
if (el["on" + type]) {
Handlers[0] = el["on" + type];
}
}
Add callback function
Handlers.push (Handler)
Bind our handler functions in a onxxxx way
El["on" + type] = Dom.handleevent;
};
Dom.detachevent = function (el, type, handler) {
if (el.events && El.events[type]) {
El.events[type].remove (Handler)
}
}
Dom.handleevent = function (event) {
var returnvalue = true;
Grab the Event object (IE uses a global event object)
Event = Event | | Fixevent ((this.ownerdocument | | this.document | | this). parentwindow | | window). event);
Get a reference to the hash table of event handlers
var handlers = This.events[event.type];
Execute each event handler
for (Var i=0,n=handlers.length;i<n;i++) {
if (Handlers[i] (event) = = False) {
ReturnValue = false;
}
}
Return returnvalue;
};
function Fixevent (event) {
Add Consortium standard Event methods
Event.preventdefault = Fixevent.preventdefault;
Event.stoppropagation = fixevent.stoppropagation;
return event;
};
Fixevent.preventdefault = function () {
This.returnvalue = false;
};
Fixevent.stoppropagation = function () {
This.cancelbubble = true;
};
var $ = function (ID) {
return document.getElementById (ID)
}
Window.onload = function () {
var a = function (e) {
$ ("P"). InnerHTML = E.clientx + "" +e.clienty
}
Dom.attachevent ($ ("target"), "MouseMove", a);
settimeout (function () {
Dom.detachevent ($ ("target"), "MouseMove", a);
},10*1000)
}
</script>
<body>
<div id= "Target" >
</div>
<p id= "P" ></p>
</body>


Dom.handleevent = function (event) {
Event = Event | | window.event
Event = dom.fixevent (event);
Event.currenttarget = this;//fixed Currenttarget
var returnvalue = true;
var handlers = This.events[event.type];
for (Var i=0,n=handlers.length;i<n;i++) {
if (Handlers[i] (event) = = False) {
ReturnValue = false;
}
}
return returnvalue;
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.