This article is a collection and collation of some js classic instances. It is very useful for reference. You need to know about them together. This article is a collection and collation of some js classic instances, I would like to share with you the script home platform for your reference!
Add events across browsers
// Add event function addEvent (obj, type, fn) {if (obj. addEventListener) {obj. addEventListener (type, fn, false);} else if (obj. attachEvent) {// IEobj. attchEvent ('on' + type, fn );}}
Cross-browser removal event
// Remove event function removeEvent (obj, type, fn) {if (obj. removeEventListener) {obj. removeEventListener (type, fn, false);} else if (obj. detachEvent) {// compatible with IEobj. detachEvent ('on' + type, fn );}}
Prevent default behavior across browsers
// Disable the default function preDef (ev) {var e = ev | window. event; if (e. preventDefault) {e. preventDefault ();} else {e. returnValue = false ;}}
Cross-browser retrieval of target objects
// Function gettarget(ev1_if(ev.tar get) {// w3creturn ev.tar get;} else if (window. event. srcElement) {// IEreturn window. event. srcElement ;}}
Cross-browser acquisition of the scroll bar location
// Obtain the scroll bar position across browsers. sp = scroll positionfunction getSP () {return {top: document.doc umentElement. scrollTop | document. body. scrollTop, left: document.doc umentElement. scrollLeft | document. body. scrollLeft ;}}
Obtain the visible window size across browsers
// Obtain the visible window size across browsers function getWindow () {if (typeof window. innerWidth! = 'Undefined') {return {width: window. innerWidth, height: window. innerHeight} else {return {width: document.doc umentElement. clientWidth, height: document.doc umentElement. clientHeight }}},
Js object impersonating