Routine collection and sorting of JavaScript classic instances (Common classic) _ javascript skills

Source: Internet
Author: User
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

Related Article

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.