JS add events, remove events, block bubbles, Block browser default behavior, and so on (compatible with Ie/ff/chrome)

Source: Internet
Author: User

Online about this aspect of the code is more and more chaotic, here to tidy up and improve. Add Event
var addevent = function (obj, type, fn) {  if (obj.addeventlistener)      obj.addeventlistener (type, FN, false);  else if (obj.attachevent) {      obj["e" +type+fn] = fn;      Obj.attachevent ("On" +type, function () {          obj["E" +type+fn].call (obj, window.event);      });}  ;
removing events
var removeevent = function (obj, type, fn) {  if (obj.removeeventlistener)      obj.removeeventlistener (Type, FN, Fals e);  else if (obj.detachevent) {      obj.detachevent (  "on" +type, obj["E" +type+fn]);      obj["E" +type+fn] = null;  }};
Block events (including bubbling and default behavior)
var stopevent = function (e) {    e = e | | window.event;    if (e.preventdefault) {      e.preventdefault ();      E.stoppropagation ();    } else{      e.returnvalue = false;      E.cancelbubble = true;    }  },
Block event bubbling only
var stoppropagation = function (e) {   e = e | | window.event;  if (!+ "\v1″) {     e.cancelbubble = true;  } else {     e.stoppropagation ();}  }
Block browser default behavior only
var  preventdefault  = function (e) {    e = e | | window.event;    if (e.preventdefault) {      e.preventdefault ();    } else{      e.returnvalue = false;    }}

Get Event Source Object
var geteventtarget = function (e) {   e = e | | window.event;  var target = event.srcelement? Event.srcElement:event.target;  return target;}
Attached: Binding Onpropertychange event Onpropertychange, which is triggered when an element's attribute changes, is typically used to capture its value change in a FORM element, which captures its change more in real time than the onchange event, but for Microsoft private events. FF is roughly similar to its Oninput event, but it only targets the Value property of TextField and textarea. Both Safari,firefox,chrome and opera support this event.
var addpropertychangeevent = function (OBJ,FN) {  if (window. ActiveXObject) {      obj.onpropertychange = fn;  } else{      obj.addeventlistener ("input", fn,false);}  }

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.