/**
* @ Author Super Sha
* QQ: 770104121
* E-Mail: supei_shafeng@163.com
* Publish date: 2009-3-27
* All Rights Reserved
*/
Var JsHelper ={}; // declare a JsHelper root namespace
JsHelper. DOM ={}; // declare the DOM namespace under the JsHelper directory
JsHelper. Event ={}; // declare the Event namespace under the JsHelper directory
JsHelper. Browser ={}; // declare the Browser-related function Browser namespace under the JsHelper directory
JsHelper. Ajax ={}; // declare the Ajax-related function namespace under the Jshelper directory
JsHelper. String ={}; // declare the String-related namespace under the JsHelper directory
/*
* $ () You can enter multiple parameters. An array of retrieved objects is returned.
*/
Var $ = function () {// simplified $ Method
Var elements = new Array ();
If (arguments. length = 0) {// if the parameter is null, the document element is returned.
Return document;
}
For (var I = 0; I <arguments. length; I ++)
{
Var element = arguments [I];
If (typeof element = string)
{
Element = document. getElementById (element );
}
If (arguments. length = 1)
{
Return element;
}
Elements. push (element );
}
Return elements;
}
JsHelper. DOM. $ = function ()
{
Var elements = new Array ();
For (var I = 0; I <arguments. length; I ++)
{
Var element = arguments [I];
If (typeof element = string)
{
Element = document. getElementById (element );
}
If (arguments. length = 1)
{
Return element;
}
Elements. push (element );
}
Return elements;
}
/*
* $ Value () can be input with multiple parameters. An array of values for obtaining objects is returned.
*/
JsHelper. DOM. value = function ()
{
Var values = new Array ();
For (var I = 0; I <arguments. length; I ++)
{
Var element = arguments [I];
If (typeof (element) = string)
{
Var v = document. getElementById (element). value;
}
If (arguments. length = 1)
{
Return v;
}
Values. push (v );
}
Return values;
}
/*
MakeArray generates an array for the input parameters. If the parameter is null, "undefined" is returned. Otherwise, an array is returned.
*/
JsHelper. String. makeArray = function ()
{
Var values = new Array ();
If (arguments. length> 0 ){
For (var I = 0; I <arguments. length; I ++)
{
Var element = arguments [I];
If (typeof element = "string ")
{
Values. push (element );
}
}
}
Else
{
Return "undefined ";
}
Return values;
}
/*
* Declare a StringBuilder class to handle connection string performance issues
*/
JsHelper. String. StringBulider = {
_ Strs: new Array (),
Append: function (str) {// Add a string to attribute _ strs
This. _ strs. push (str );
Return this;
},
ToString: function (){
If (arguments. length! = 0 ){
Return this. _ strs. join (arguments [0]); // return the string after attribute _ strs is combined. An optional parameter is accepted for join.
} Else {
Return this. _ strs. join ("");
}
}
};
/*
* $ TagName () input a parameter and an array is returned for obtaining the Elements TagNeme object.
*/
JsHelper. DOM. tagName = function ()
{
Var element = arguments [0];
If (typeof element = string)
{
Var tagname = document. getElementsByTagName (element );
}
Return tagname;
}
// ================================================ ======================================
/*
* Label: HTML Label ID
* Applicable only to responseText
* Only applicable to the GET Method
*/
Var _ xmlhttp; // declare a global XMLHttpRequest object instance
Function Ajax (method, url, label ){
This. method = method;
This. url = url;
Try {
_ Xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP ");
}
Catch (e ){
Try {
_ Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (s ){
_ Xmlhttp = new XMLHttpRequest ();
}
}
Ajax. prototype. ResponseText = function (){
_ Xmlhttp. onreadystatechange = this. onComplete;
_ Xmlhttp. open (this. method, this. url, true)
_ Xmlhttp. send (null );
}
Ajax. prototype. onComplete = function (){
If (_ xmlhttp. readyState = 4 ){
If (_ xmlhttp. status = 200 ){
$ (Label). innerHTML = _ xmlhttp. responseText;
}
}
}
This. ResponseText ();
}
// ================================================ ============
/*
* Determine the browser type
*/
Var ua = navigator. userAgent. toLowerCase ();
If (window. ActiveXObject ){
JsHelper. Browser. IE = ua. match (/msie ([\ d.] +)/) [1];
}
Else if (document. getBoxObjectFor ){
JsHelper. Browser. Firefox = ua. match (/firefox \/([\ d.] +)/) [1];
}
Else if (window. MessageEvent &&! Document. getBoxObjectFor ){
JsHelper. Browser. Chrome = ua. match (/chrome \/([\ d.] +)/) [1];
}
Else if (window. opera ){
JsHelper. Browser. Opera = ua. match (/opera. ([\ d.] +)/) [1];
}
Else if (window. openDatabase ){
JsHelper. Browser. Safari = ua. match (/version \/([\ d.] +)/) [1];
}
/*
* Declare an instance of the XMLHttpRequest object and return the instance
*/
JsHelper. Ajax. createRequest = function ()
{
Var xmlhttp = null;
Try
{
Xmlhttp = new XMLHttpRequest ();
}
Catch (trymicrosoft ){
Try {
Xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP ");
}
Catch (othermicrosoft ){
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
}
Return xmlhttp;
}
/*
* A common AddEventListener function. obj is a DOM element.
*/
JsHelper. Event. addEventlistener = function (labelID, eventMode, fn)
{
Var obj = JsHelper. DOM. $ (labelID );
If (typeof window. addEventListener! = Undefined ){
Obj. addEventListener (eventMode, fn, false );
}
Else
If (typeof document. addEventListener! = Undefined ){
Obj. addEventListener (eventMode, fn, false );
}
Else
If (typeof window. attachEvent! = Undefined ){
Obj. attachEvent ("on" + eventMode, fn );
}
Else {
Return false;
}
Return true;
}
/*
* It contains an extended method of the function by Douglas Crockford. The following three functions are copyrighted by Douglas Crockford and are hereby declared
*/
Function. prototype. method = function (name, func ){
This. prototype [name] = func;
Return this;
};
Function. method (inherits, function (parent ){
Var d = {}, p = (this. prototype = new parent ());
This. method (base, function uber (name ){
If (! (Name in d )){
D [name] = 0;
}
Var f, r, t = d [name], v = parent. prototype;
If (t ){
While (t ){
V = v. constructor. prototype;
T-= 1;
}
F = v [name];
} Else {
F = p [name];
If (f = this [name]) {
F = v [name];
}
}
D [name] + = 1;
R = f. apply (this, Array. prototype. slice. apply (arguments, [1]);
D [name]-= 1;
Return r;
});
Return this;
});
Function. method (swiss, function (parent ){
For (var I = 1; I <arguments. length; I + = 1 ){
Var name = arguments [I];
This. prototype [name] = parent. prototype [name];
}
Return this;
});
/*
* Solution: IE does not support HTMLElement.
*/
Var DOMElement = {
Extend: function (name, fn)
{
If (! Document. all)
{
Eval ("HTMLElement. prototype." + name + "= fn ");
}
Else
{
Var _ createElement = document. createElement;
Document. createElement = function (tag)
{
Var _ elem = _ createElement (tag );
Eval ("_ elem." + name + "= fn ");
Return _ elem;
}
Var _ getElementById = document. getElementById;
Document. getElementById = function (id)
{
Var _ elem = _ getElementById (id );
Eval ("_ elem." + name + "= fn ");
Return _ elem;
}
Var _ getElementsByTagName = document. getElementsByTagName;
Document. getElementsByTagName = function (tag)
{
Var _ arr = _ getElementsByTagName (tag );
For (var _ elem = 0; _ elem <_ arr. length; _ elem ++)
Eval ("_ arr [_ elem]." + name + "= fn ");
Return _ arr;
}
}
}
};
/*
* The following describes several DOM query functions modeled by John Resig, the father of jQuery.
*/
DOMElement. extend ("previous", function () {// similar to previussibling DOM Function
Var elem = this;
Do {
Elem = elem. previussibling;
} While (elem & elem. nodeType! = 1 );
Return elem;
});
DOMElement. extend ("next", function () {// similar to nextSibling DOm Function
Var elem = this;
Do {
Elem = elem. nextSibling;
} While (elem & elem. nodeType! = 1 );
Return elem;
});
DOMElement. extend ("first", function (num) {// similar to firstChild DOM Function, same as parent
Var elem = this;
Num = num | 1;
For (var I = 0; I <num; I ++ ){
Elem = elem. firstChild;
}
Return (elem & elem. nodeType! = 1? Next (elem): elem );
});
DOMElement. extend ("last", function (num) {// similar to lastChild DOM Function, same as parent
Var elem = this;
Num = num | 1;
For (var I = 0; I <num; I ++ ){
Elem = elem. lastChild;
}
Return (elem & elem. nodeType! = 1? Prev (elem): elem );
});
DOMElement. extend ("parent", function (num) {// returns parentnodes of several num levels. For example, parent (2) is equivalent to elem. parent (). parent ();
Var elem = this;
Num = num | 1;
For (var I = 0; I <num; I ++ ){
If (elem! = Null ){
Elem = elem. parentNode;
}
}
Return elem;
});
DOMElement. extend ("hasChilds", function () {// a subnode exists when determining
If (this! = Null & this. hasChildNodes ()){
Return true;
}
Else {
Return false;
}
});
DOMElement. extend ("text", function () {// obtain the text in the tag. If the parameter is not zero, you can set the text in the tag, which is also suitable for the input tag.
Try {// solution Firefox does not support InnerText
HTMLElement. prototype. _ defineGetter _ ("innerText", function (){
Var anyString = "";
Var childS = this. childNodes;
For (var I = 0; I <childS. length; I ++ ){
If (childS [I]. nodeType = 1 ){
AnyString + = childS [I]. tagName = "BR "? "N: childS [I]. innerText;
}
Else if (childS [I]. nodeType = 3 ){
AnyString + = childS [I]. nodeValue;
}
}
Return anyString;
});
}
Catch (e ){}
If (arguments. length = 1 ){
If (this. innerText ){
This. innerText = arguments [0];
}
Else {
This. value = arguments [0];
}
}
Else {
Return this. innerText | this. value;
}
});
DOMElement. extend ("html", function () {// obtain the innerHTML of the element. If the parameter is not zero, you can set the text and subnodes in the element.
If (arguments. length = 0 ){
Return this. innerHTML;
}
Else if (arguments. length = 1)
{
This. innerHTML = arguments [0];
}
});
/*
* The following is the className operation.
*/
DOMElement. extend ("getClassName", function () {// return the element className
If (this! = Null & this. nodeType = 1 ){
Return this. className. replace (/\ s +/,). split ();
}
Return null;
});
DOMElement. extend ("hasClassName", function () {// determine whether a class exists
If (this! = Null & this. nodeType = 1 ){
Var classes = this. getClassName ();
For (var I = 0; I <classes. length; I ++ ){
If (arguments [0] = classes [I]) return true;
}
} Else {
Return false;
}
});
DOMElement. extend ("addClass", function () {// Add a class to the element. You can add multiple classes at a time.
If (this! = Null & this. nodeType = 1 ){
For (var I = 0; I <arguments. length; I ++ ){
This. className + = (this. className? :) + Arguments [I];
}
Return this;
}
Return null;
});
DOMElement. extend ("removeClass", function () {// delete a class. If no parameter exists, delete all classes.
If (this! = Null & this. nodeType = 1 ){
If (arguments. length = 0 ){
This. className = "";
}
Else if (arguments. length! = 0 ){
Var classes = this. getClassName ();
For (var I = 0; I <arguments. length; I ++ ){
For (var j = 0; j <classes. length; j ++ ){
If (arguments [I] = classes [j]) {
Classes = classes. join (""). replace (arguments [I],). split ("");
}
}
}
This. className = classes. join ("");
}
Return this;
}
Return null;
});
JsHelper. _ toggleflag = false; // adds a judgment switch.
DOMElement. extend ("toggleClass", function (classname) {// The two clicks call different functions
If (this! = Null & this. nodeType = 1 ){
This. onclick = function (){
If (JsHelper. _ toggleflag = false ){
This. addClass (classname );
JsHelper. _ toggleflag = true;
} Else if (JsHelper. _ toggleflag = true ){
This. removeClass (classname );
JsHelper. _ toggleflag = false;
}
}
}
});
/*
* Add the click method for each object, similar to the use of the click method of jQuery.
*/
DOMElement. extend ("click", function (){
If (this! = Null & this. nodeType = 1 ){
If (arguments. length = 0 ){
Alert ("you have done nothing when you clicked .");
} Else {
This. onclick = arguments [0];
}
}
});
/*
* Extend the hover method for each object. This method accepts two functions as parameters.
*/
DOMElement. extend ("hover", function (){
If (this! = Null & this. nodeType = 1 ){
If (arguments. length! = 2 ){
Alert ("Require two function to be param .");
} Else {
This. onmouseover = arguments [0];
This. onmouseout = arguments [1];
}
}
});
/*
* Function for adding events to each element
*/
DOMElement. extend ("addEvent", function (eventtype, fn ){
If (document. all ){
This. attachEvent ("on" + eventtype, fn );
} Else {
This. addEventListener (eventtype, fn, false );
}
});
/*
* Extend the css method for each element and accept an attribute and attribute value as a parameter.
*/
DOMElement. extend ("css", function (){
If (this! = Null & this. nodeType = 1 ){
If (arguments. length! = 2 ){
Alert ("Require two function to be param .");
} Else {
This. style [arguments [0] = arguments [1]; // set the value of the relevant style attribute
Return this;
}
}
Return null;
});
/*
* // Search for and return all elements of a class. The name is className, And the type is the HTML Tag type.
*/
Var hasClass = function (name, type ){
Var r = new Array ();
// Var re = new RegExp (name, "g ");
Var e = document. getElementsByTagName (type | "*");
For (var I = 0; I <e. length; I ++ ){
Var classes = e [I]. getClassName ();
For (var j = 0; j <classes. length; j ++ ){
If (name = classes [j]) {
R. push (e [I]);
}
}
}
Return r;
}
/*
* Returns the reference set of a specific child element of an element. If no element calls this method, the default value is document.
*/
DOMElement. extend ("find", function (){
Var elem = this | document;
Var r = new Array ();
If (elem! = Null & (elem. nodeType = 1 | elem. nodeType = 9 )){
Var e = elem. getElementsByTagName (arguments [0]);
For (var I = 0; I <e. length; I ++ ){
R. push (e [I]);
}
Return r;
}
Return null;
});