The event-binding function has the following characteristics:
1, in the event function, this points to the current element
2, each event is actually a sequence, in order to execute this sequence.
3, the correct pass the event object. This example fixes the event bubbling and blocking default behavior for events that can be fixed in the differences between the standard and IE of the event object.
4, this machine test through IE, Firefox, Chrome, Opera, Safari (can be said general mainstream browser)
5. Support to delete event bindings
----------Event bindings and delete bindings----------//
function bindevent (element, EventName, func) {
var events = element[' the ' +eventname]; Used to save an event sequence
if (!events) {//If a sequence is not present, create it and add the binding in the form of onevent = function () {} in the HTML tag
Events = element[' the ' +eventname] = [];
if (element[' on ' +eventname]) {Events.push (element[' on ' +eventname]);
}
Detect if duplicate binding
for (var i=0; i<events.length; i++) {
if (events[i] = = Func) {flag = true;
}
Non-repeating binding, the function event is added
if (i >= events.length) {Events.push (func);}
Redefine how this event is executed
element[' on ' +eventname] = function (event) {
Event = Event | | (function () {//fix IE's Event object
var e = window.event;
E.preventdefault = function () {e.returnvalue = false;}
E.stoppropagation = function () {e.cancelbubble = true;}
Continue to repair as needed
return e;
})();
Perform these functions sequentially
for (var i=0 i<events.length; i++) {Events[i].call (element, event);}
}
}
Delete Event Bindings
function unbindevent (element, EventName, func) {
var events = this[' the ' +eventname];
If a sequence of events does not exist
if (!events) {return false;}
Detects whether the function exists in the sequence of events
for (var i=0; i<events.length; i++) {
if (func = = Events[i]) {
[].splice.call (Events, I, 1);
return true;
}
}
function does not exist in this sequence of events
return false;
}
Test code
<script type= "text/web Effects" src= "Event.js" ></SCRIPT>
<script type= "Text/javascript"
Bindevent (window, ' Load ', function () {
var ul = document.getElementById ("test");
var a = document.getElementById ("Baidu");
bindevent (UL, ' click ', Function (e) {
alert ("I am" + this + "element, you clicked on me!");
});
bindevent (A, ' click ', Function (e) {
& nbsp; alert ("I am" + this +), you clicked on me! ");
});
});
</script>
<body>
<ul id= "Test" >
<p class= "T" >hello,world!</p>
<p>hello,<a href= "Http://www.111cn.net" id= "Baidu" > </a>!</p>
<li id= "haha" class= "T" >afasdfsa</li>
<li>sfk</li>
<li class= "T" >sdklfajsfjk</li>
<li>end</li>
<li class= "TE" >of</li>
</ul>
<ol id= "Test2" >
<li class= "T" > the first item added </li>
<li class= "T" > added second item </li>
<li class= "T" > Additional third item </li>
</ol>
The above code testing can be done through the current mainstream browsers such as the native test through IE, Firefox, Chrome, Opera, Safari (can be said general mainstream browser)