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 ');