Javascript code of a cssQuery object

Source: Internet
Author: User

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.

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.