Home > Web Front-end > JS Tutorial > javascript getElementsByClassName and js get address bar parameters_javascript skills

javascript getElementsByClassName and js get address bar parameters_javascript skills

WBOY
Release: 2016-05-16 18:37:30
Original
956 people have browsed it

getElementsByClassName()
In order to find our tree menu (perhaps multiple) from a lot of HTML code, we first implement a method to find DOM nodes through className: getElementsByClassName. This is a simple but useful extension to the browser's native DOM methods.

This method has two parameters: ele points out which DOM node is the root node to search for (that is, only the child nodes of ele are found), and className points out what className must be included in the class attribute of the node that meets the conditions. . Its return value is an array that stores all nodes that meet the conditions.

Copy code The code is as follows:

function getElementsByClassName(ele,className) {
/ /Get all child nodes
if(document.all){
var children = ele.all;
}else{
var children = ele.getElementsByTagName('*');
}
//Traverse the child nodes 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 statement is for compatibility with IE5 (IE5 cannot run
document.getElementsByTagName('*')). It should be noted that you should never use the browser detection method to write a script. Instead, you should directly use the statements to be used to test whether it can be executed. If the return value is null or undefined, then use another method. Such scripts can have better compatibility and be more robust.
elements[elements.length] = child;, this sentence is also for compatibility with IE5, so the push method of array
is not used. If you must use the push method, you can overload the push method before executing getElementsByClassName()
. The code is as follows:

Array.prototype.push = function(value){
this[this.length] = value;
} Note: Originally, I hoped that getElementsByClassName could also be written like the push method, For example
HTMLElement.prototype.getElementsByClassName = .... However, during actual operation, I found that the HTMLElement object was not fixed when
was running. Each tag seemed to be different, so I had to give up.

Get address bar parameters
Copy code The 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];
}
//Get the passed name Parameters
name=URLParams['name'];

//v2:
Request = {
QueryString : function(item){
var svalue = location.search.match (new
RegExp('[?&]' item '=([^&]*)(&?)','i'));
return svalue ? svalue[1] : svalue;
}
}
var key = Request.QueryString('key');
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template