A Cssquery object JavaScript script implements code _js object oriented

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

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.