First: A simple universal approach (IE && FF)
Window.onload = function () {
var odiv = document.getElementById ("J_mydiv"); Find Object
Odiv.onclick = function () { //Set event listener functions
Alert ("click");
}
}
Cons: 1. Adding a single event
2. Cannot delete event
Second: The listener function in IE
var odiv;
function Fnclick () {
Alert ("click Me");
Odiv.detachevent ("onclick", fnclick);//delete listener function
}
Window.onload = function () {
Odiv = document.getElementById ("J_mydiv");//Find Object
Odiv.attachevent ("onclick", Fnclick); Adding a listener function
}
Third: Standard DOM listener function
var odiv;
function FnClick1 () {
Alert ("Click1");
Odiv.removeeventlistener ("click", Fnclick2,false); Deleting a listener function 2
}
function FnClick2 () {
Alert ("Click2");
}
Window.onload = function () {
Odiv = document.getElementById ("J_mydiv"); Find Object
Odiv.addeventlistener ("click", Fnclick1,false); Adding a listener function 1
Odiv.addeventlistener ("click", Fnclick2,false); Adding a listener function 2
}
Four: From the JS instance view event monitoring see: http://imethan.com/?p=208
Five: From the example of JS event Monitoring Learning Notes (event monitoring bindings, ff/ie different processing mechanisms for compatibility summary) See: http://hi.baidu.com/dou917/blog/item/40219f37e6e3deee1b4cff67.html
Sixth: Code Summary
<script language= "JavaScript" >
Javascript Event Demo
Window.onload = function () {
var hidebox = function (event) {
document.getElementById (' Status_show '). style.display = ' None ';
document.getElementById (' Status_hide '). style.display = ' block ';
};
var Showbox = function (event) {
document.getElementById (' Status_show '). style.display = ' block ';
document.getElementById (' Status_hide '). style.display = ' None ';
Stopevent (event);
};
var stopevent = function (event) {
E = Event | | window.event;
if (e.stoppropagation) {
E.stoppropagation ();
}else {
E.cancelbubble = true;
}
};
if (Document.addeventlistener) {
Document.addeventlistener (' Click ', Hidebox, false);
document.getElementById (' Status_hide '). AddEventListener (' Click ', Showbox, false);
document.getElementById (' Status_show '). AddEventListener (' Click ', Stopevent, false);
}else {
For IE
Document.attachevent (' onclick ', hidebox);
document.getElementById (' Status_hide '). attachevent (' onclick ', showbox);
document.getElementById (' Status_show '). attachevent (' onclick ', stopevent, Showbox);
}
};
</script>
This notation cannot bind multiple events at the same time
This is how the code is coupled together
AddEventListener () is a method defined in the DOM2 standard that can control whether an event handler is invoked during the event capture phase or during the bubbling phase (determined by the third parameter of the function True/false), since this is defined in the DOM2 standard, This method is supported only by browsers that support DOM2-level event handlers (both Ie9,firefox,safari,chrome and opera support this).
The Attachevent () method is not defined by the DOM standard, but is implemented by IE itself, and since IE8 and previous versions only support event bubbling, it is conceivable that the event handlers added by this method will only be called during the bubbling Phase, AddEventListener () and attachevent () Another difference is the first parameter-the event type, attachevent () event type is preceded by ' on ', such as click event Click, AddEventListener () can pass "click", and Attachevent () but to pass the ' onclick ';
AddEventListener and Attachevent