Some function encapsulation 1

Source: Internet
Author: User

Package document.getElementById
function $ () {
var elements = new Array ();

for (var i = arguments.length-1; I >= 0; i--) {
var element = Arguments[i];

if (typeof element = = "string") {
element = document.getElementById (element);
}

if (arguments.length = = 1) {
return element;
} else {
Elements.push (Element);
}

return elements;
};
};

Encapsulating event handling (resolving compatibility)
function addevent (node, type, listener) {
if (! ( node = $ (node)) return false;
if (Node.addeventlistener) {
W3c
Node.addeventlistener (type, listener, false);
return true;
} else if (node.attachevent) {
Node[' E ' + type + listener] = listener;
Node[type + Listener] = function () {
Node[' E ' + type + listener] (window.event);
};
Node.attachevent (' on ' + type, Node[type + listener]);
return true;
}

return false;

};

Move Out Event
function Removeevent (node,type,listener) {
if (! ( node = $ (node)) return false;
if (Node.removeeventlistener) {
Node.removeeventlistener (Type,listener,false);
return true;
}else if (node.removeevent) {
Node.detachevent (' on ' +type,node[type+listener]);
Node[type+listener] = null;
return true;
}
return false;
};
Get Event Object
function Geteventobject (e) {
Return E | | window.event;

}
Block event bubbling
function Stoppropagation (eventobject) {
var eventobject = EventObject | | Geteventobject ();
if (eventobject.stoppropagation) {
Eventobject.stoppropagation ();
}else{
Eventobject.cancelbubble = true;
}
};
Block Browser Default Events
function Stopdefault (eventobject) {
var eventobject = EventObject | | Geteventobject ();
if (Eventobject.preventdefault) {
Eventobject.preventdefault ();
}else{
Eventobject.returnvalue = false;
}
};
Gets the coordinates of the mouse over the entire document
function Getpointerpositionindocument (eventobject) {
EventObject = EventObject | | Geteventobject (EventObject);
var x = Eventobject.pagex | | (Eventobject.clientx +
(Document.documentElement.scrollLeft | | document.body.scrollLeft));
var y = Eventobject.pagey | | (Eventobject.clienty +
(Document.documentElement.scrollTop | | document.body.scrollTop));
return {
' X ': X,
' Y ': Y
};
};
Get window width
function Getwindowsize () {
var width = height = 0;
if (this.innerwidth) {
width = this.innerwidth;
Height = this.innerheight;
}else if (document.documentelement && document.documentElement.clientHeight) {
width = document.documentElement.clientWidth;
Height = document.documentElement.clientHeight;
}else if (document.body && document.body.clientHeight) {
width = document.body.clientWidth;
Height = document.body.clientHeight;
}

return {' width ': width, ' height ': height};
};
Gets the position and height of the given label
function Getdimensions (Element) {
if (! ( Element = $ (Element))) return false;

return {
' Left ': Element.offsetleft,
' Top ': element.offsettop,
' Width ': element.offsetwidth,
' Height ': element.offsetheight
};
}
Set CSS Styles
function SetStyle (element, styles) {
if (! ( Element = $ (Element))) return false;
For (property in styles) {
if (!styles.hasownproperty) continue;

if (Element.style.setProperty) {
Element.style.setProperty (
Uncamelize (property, '-'), Styles[property], NULL);
} else {
Element.style[camelize (property)] = Styles[property];
}
}
return true;
};
Remove the bar and use the Hump naming method
function Camelize (s) {
Return S.replace (/-(\w)/g, function (Strmatch, p1) {
return P1.touppercase ();
});
};
Convert the Hump naming method to a cross-bar name
function Uncamelize (s, Sep) {
Sep = Sep | | ‘-‘;
Return S.replace (/([A-z]) ([A-z])/g, function (Strmatch, p1, p2) {
return p1 + Sep + p2.tolowercase ();
});
};


function GetEvent (ARG) {
var Eve;
if (window.event)
Eve = window.event;
Else
Eve = arg[0];
return eve;
}

Some function encapsulation 1

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.