Copy Code code as follows:
/**
* @author Supersha
* @QQ: 770104121
*/
var cssquery = {
Parent: The reference used to store the parent node of the current node
Parent:document,
Select:function (SELECTORSTR) {
var selectors=selectorstr.split (""); Separating strings
for (var i = 0, len = selectors.length i < len; i++) {
var el = This.parent | | Document The node reference used to store the specified class attribute
var val=this.replacestr (Selectors[i]); Instead of "#" and "." The point number used to get the node reference for the specified ID
if (selectors.length = = 1) {//If only one argument
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 ID or class attribute
Return (this. Idlabel (Selectors[i])? This.$ (val): This.getelementsbyclassname (document, "*", Val);
}
}
If the last ID or class or HTML tag in the Selectorstr word symbol is reached
else if (i = = selectors.length-1) {
if (!) ( /[#.] /g). Test (Selectors[i]) {//If it is an HTML tag
Return El.getelementsbytagname (Selectors[i]);
}
else {//If the ID or class attribute
Return (this. Idlabel (Selectors[i])? This.$ (val): This.getelementsbyclassname (el, "*", Val);
}
}
else{///If there are more than two levels of SELECTORSTR, then store the current node's reference to the parent property
if (!) ( /[#.] /g). Test (Selectors[i]) {//If it is an HTML tag
This.parent = El.getelementsbytagname (selectors[i]) [0];
}
else {//If the ID or class attribute
This.parent = ((/#/gi). Test (Selectors[i])? This.$ (val): el;
}
}
}
},
$: function (ID) {//used to get a reference to the specified ID
return document.getElementById (ID);
},
Idlabel:function (selector) {//Determine if ID attribute
Return ((/#/GI). Test (selector)? True:false;
},
Classlabel:function (selector) {//judge whether the class attribute
Return ((/\./GI). Test (selector)? True:false;
},
Replacestr:function (a) {//Replace "#" and "." The point number used to get the node reference for the specified ID
Return A.replace ("#", ""). Replace (".", "");
},
Getelementsbyclassname:function (EL, tag, classname) {//Get a reference to the element containing the class attribute value through 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)) {//to detect the class name based on the regular
Matchelements.push (Element);
}
}
return matchelements;
}
}
Call method: Cssquery.select (selectorstring); Selectorstring like this: "#p #b. Em",
You can receive a combination of HTML tags and ids, class, and return a reference to the specified selectorstring