Javascript getelementsbyclassname and JS get the address bar Parameters

Source: Internet
Author: User

Getelementsbyclassname ()
To read a large number of HTML Code To find out the tree menu (maybe there are multiple), let's first implement a method to find the DOM node through classname: getelementsbyclassname. This is a simple but practical extension of the browser's own DOM method.

This method has two parameters: ele indicates which Dom node is used as the root node (that is, only the child node of ELE is used ), classname indicates the type of classname that must be included in the class attribute of a qualified node. Its return value is an array that stores all nodes that meet the conditions.Copy codeThe Code is as follows: function getelementsbyclassname (Ele, classname ){
// Obtain all child nodes
If (document. All ){
VaR children = ELE. All;
} Else {
VaR children = ELE. getelementsbytagname ('*');
}
// Traverse the subnode and check the classname attribute
VaR elements = new array ();
For (VAR I = 0; I <children. length; I ++ ){
VaR child = children [I];
VaR classnames = Child. classname. Split ('');
For (var j = 0; j <classnames. length; j ++ ){
If (classnames [J] = classname ){
Elements [elements. Length] = child;
Break;
}
}
}
Return elements;
}

VaR trees = getelementsbyclassname (document, 'treeview ');

The first if-else language is to be compatible with ie5 (ie5 cannot run
Document. getelementsbytagname ('*')). Note that do not use the browser detection method to write scripts. Instead, you should directly use the statements to test whether execution can be performed. If the return value is null or undefined, then another method is used. Such scripts can have better compatibility and robustness.
Elements [elements. Length] = child;
. If you must use the push method, You can execute getelementsbyclassname ()
Previously, reload the push method. The Code is as follows:

Array. Prototype. Push = function (value ){
This [This. Length] = value;
} Note: I originally hoped that getelementsbyclassname could be written like a push method, for example
Htmlelement. Prototype. getelementsbyclassname = .... However, during actual operations
The htmlelement object is not fixed during runtime, and each tag seems to be different.

Parameters in the address barCopy codeThe Code is as follows: // V1:
VaR urlparams = new array ();
VaR aparams = Document. Location. Search. substr (1). Split ('&');
For (I = 0; I <aparams. length I ++ ){
VaR aparam = aparams. Split ('= ');
Urlparams [aparam [0] = aparam [1];
}
// Obtain the passed name parameter
Name = urlparams ['name'];

// V2:
request = {
querystring: function (item) {
var svalue = location. search. match (New
Regexp ('[\? \ &] '+ Item +' = ([^ \ &] *) (\ &?) ',' I ');
return svalue? Svalue [1]: svalue;
}< BR >}< br> var key = request. querystring ('key');

Related Article

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.