Copy codeThe Code is as follows:
/**
* @ Author Supersha
* @ QQ: 770104121.
*/
Var cssQuery = {
// Parent: used to store the reference of the parent node of the current node
Parent: document,
Select: function (selectorStr ){
Var selectors = selectorStr. split (""); // delimiter string
For (var I = 0, len = selectors. length; I <len; I ++ ){
Var el = this. parent | document; // used to store node references of the specified class attribute
Var val = this. replaceStr (selectors [I]); // Replace the "#" and "." periods to obtain the node reference of the specified ID.
If (selectors. length = 1) {// if there is only one parameter
If (! (/[#.]/G). test (selectors [I]) {// if it is an HTML Tag
Return document. getElementsByTagName (selectors [I]);
}
Else {// if it is an ID or a specified class value
// Determine whether the attribute is ID or class
Return (this. IDLabel (selectors [I])? This. $ (val): this. getElementsByClassName (document, "*", val );
}
}
// If the value reaches the last ID, class, or HTML tag in the selectorStr symbol
Else if (I = selectors. length-1 ){
If (! (/[#.]/G). test (selectors [I]) {// if it is an HTML Tag
Return el. getElementsByTagName (selectors [I]);
}
Else {// for ID or class attributes
Return (this. IDLabel (selectors [I])? This. $ (val): this. getElementsByClassName (el, "*", val );
}
}
Else {// if there are two or more selectorStr levels, the reference of the current node is stored in the parent attribute.
If (! (/[#.]/G). test (selectors [I]) {// if it is an HTML Tag
This. parent = el. getElementsByTagName (selectors [I]) [0];
}
Else {// for ID or class attributes
This. parent = (// #/gi). test (selectors [I])? This. $ (val): el;
}
}
}
},
$: Function (id) {// used to obtain the reference of the specified ID
Return document. getElementById (id );
},
IDLabel: function (selector) {// determines whether it is an ID attribute.
Return (/#/gi). test (selector ))? True: false;
},
ClassLabel: function (selector) {// determines whether it is a class attribute.
Return (/\./gi). test (selector ))? True: false;
},
ReplaceStr: function (a) {// Replace the "#" and "." dots to obtain the node reference of the specified ID
Return a. replace ("#", ""). replace (".","");
},
GetElementsByClassName: function (el, tag, classname) {// you can use the class attribute value to obtain references to elements containing the class attribute value.
Var elem = el | document;
If (! Classname)
Return;
Tag = tag | "*";
Var allTagsDom = (tag = "*") & (elem. all ))? Elem. all: elem. getElementsByTagName (tag );
Classname = classname. replace (/\-/g ,"\\-");
Var regex = new RegExp ("(^ | \ s *)" + classname + "(\ s * | $ )");
Var matchElements = new Array ();
Var element;
For (var I = 0; I <allTagsDom. length; I ++ ){
Element = allTagsDom [I];
If (regex. test (element. className) {// check the class name based on the regular expression.
MatchElements. push (element );
}
}
Return matchElements;
}
}
// Call method: cssQuery. select (selectorString); selectorString is like this: "# p # B. em ",
// Receives the combination of HTML tags, IDs, and classes and returns the reference of the specified selectorString.