<!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;
};