Today because I have to use to write a JS to get the UL div all Li tags, the first is to get the specified ID under all the Li title, and then use length to get the length of the array, and then for one by one to traverse the innerHTML value of Li.
<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<TITLE>JS get ul div all Li tags </title>
<body>
<div id= "divID" >
<li>li1</li>
<li>li1</li>
<li>li1</li>
<li>li1</li>
</div>
<script>
var tablist = document.getElementById (' divID '). getElementsByTagName ("Li");
var lilength=tablist.length;
var maxrd = 2;
var strhtml= ';
if (Lilength > Maxrd)
{
var lilength=maxrd;
}
For (i=0 i <lilength; i++) {
strhtml = strhtml+ ' <li> ' +tablist[i].innerhtml+ ' </li> ';
}
document.write (strhtml);
</script>
</body>
<!--
If it is to obtain the UL below the LI to use the following code
var tree1 = document.getElementsByTagName ("ul") [0].childnodes;
for (var i = 0;i < tree1.length;i++) {
document.write (tree1[i].innerhtml);
}
This example uses the correlation function simple reference
getElementsByTagName gets an HTML tag name
Length lengths, for arrays, string lengths
innerHTML is generally to get window div p span a li ul etc text content
For loop
Use a For loop when the number of times the script is run is determined.
Grammar:
for (variable = start value; variable <= end value; variable = variable + step value)
{
Code that needs to be executed
}
Web page Special effects cycle
When writing code, you often want to execute the same piece of code over and over again. We can use loops to do this, so we don't have to write several lines of the same code over and over again.