JavaScript getelementsbyclassname and JS fetch address bar parameters _javascript Tips

Source: Internet
Author: User
Getelementsbyclassname ()
To find our tree menu (perhaps multiple) from a large stack of HTML code, we first implement a way to find a DOM node through classname: Getelementsbyclassname. This is a simple but practical extension of the browser's own DOM approach.

This method has two parameters: Ele indicates which DOM node to look for (that is, only ele child nodes), classname indicates what classname must be included in the class attribute of the eligible node. Its return value is an array that holds all eligible nodes.
Copy Code code as follows:

function Getelementsbyclassname (ele,classname) {
Get all child nodes
if (document.all) {
var children = Ele.all;
}else{
var children = ele.getelementsbytagname (' * ');
}
Traversing child nodes and checking classname properties
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 you should never write a script using the browser detection method, but instead use the statement you want to use to test whether it can be executed, or if the return value is null or undefined, then another way. Such scripts can be better compatible and more robust.
Elements[elements.length] = child;, this sentence is also to be compatible IE5 not using an array
Push method. If you must use the Push method, you can perform getelementsbyclassname ()
Overload the push method first. The code is as follows:

Array.prototype.push = function (value) {
This[this.length] = value;
Note: Originally I hope getelementsbyclassname can also write like the push method, such as
HTMLElement.prototype.getElementsByClassName = ..... But the actual operation was found in the
Runtime HtmlElement This object is not fixed, each kind of tag seems to be different, can only be dropped.

Take Address bar parameters
Copy Code code 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];
}
//Gets the name parameter
name=urlparams[' name ' passed over;

//v2:
Request = {
Querystring:function (item) {
var svalue = location.search.match (New
R Egexp (' [\?\&] ' + Item + ' = ([^\&]*) (\&?) ', ' I '));
return svalue svalue[1]: svalue;
}
}
var key = Request.QueryString (' key ');

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.