JS get ul div all Li tags

Source: Internet
Author: User
Tags tag name

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.

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.