JS Native Add event way:
1. Add directly to the HTML tag
<div onclick= "alert (' div ')" >div</div>
2. use the DOM's on ... method to add
document.getElementById (' div '). onclick = function () {alert (' div ')};
3. add with addeventlistener or attachevent
document.getElementById (' div '). AddEventListener (' click ', function () {alert (' div ')}, False);
native JS event binding and event removal
/**
* @description event bindings, compatible with each browser
* @param target Event Trigger object
* @param type event
* @param func event handler function
*/
function addevents (target, type, func) {
if (Target.addeventlistener)// non ie and IE9
Target.addeventlistener (Type, func, false);
AddEventListener Of course is the registration event, she has three parameters, namely:" event name ", " event callback ", " Capture / bubbling " . The last parameter is a Boolean, andtrue represents the capture event, andfalse represents the bubbling event.
else if (target.attachevent)//ie6 to IE8
Target.attachevent ("On" + Type, func);
else target["on" + type] = func; Ie5
};
/**
* @description event removal, compatible with each browser
* @param target Event Trigger object
* @param type event
* @param func event handler function
*/
function removeevents (target, type, func) {
if (Target.removeeventlistener)
Target.removeeventlistener (Type, func, false);
else if (target.detachevent)
Target.detachevent ("On" + Type, func);
else target["on" + type] = NULL;
};
/**btn.removeeventlistener (" event name ", " event callback ", " capture / bubble "); This is the same as the parameters for the binding event, which is explained in detail:
· The name of the event, which means which event to dismiss.
· An event callback is a function that must be the same as the function that registers the event.
· Event Type, Boolean, which must be the same type as when registering the event.
*/
Native JavaScript event Details: http://www.cnblogs.com/iyangyuan/p/4190773.html
JS native How to add events