JS gets all the elements of class _javascript tips

Source: Internet
Author: User
Tags tagname

Copy Code code as follows:

<script type= "Text/javascript" >

Window.onload = function ()
{var topmenus = getclass (' li ', ' Topmenu ');
for (Var i=0;i < topmenus.length; i++)
{
alert (topmenus[i].innerhtml);
}

}

function GetClass (tagname,classname)//Get the element with label name TagName, class name ClassName
{
if (document.getelementsbyclassname)//support for this function
{return document.getelementsbyclassname (className);
}
Else
{var tags=document.getelementsbytagname (tagName);//Get Label
The Var tagarr=[];//is used to return the element with the class name classname
for (Var i=0;i < tags.length; i++)
{
if (Tags[i].class = = className)
{
Tagarr[tagarr.length] = tags[i];//Save the element that satisfies the condition
}
}
return Tagarr;
}

}

</script>

<body>

<ul id= "NAV" >
<li class= "Topmenu" ><a href= "#" > Product introduction </a>
<ul class= "submenu" >
<li><a href= "#" > Products 1</a></li>
<li><a href= "#" > Products 2</a></li>
<li><a href= "#" > Products 3</a></li>
<li><a href= "#" > Products 4</a></li>
<li><a href= "#" > Products 5</a></li>
<li><a href= "#" > Products 6</a></li>
</ul>
</li>
<li class= "Topmenu" ><a href= "#" > Service Introduction </a>
<ul class= "submenu" >
<li><a href= "#" > Service 1</a></li>
<li><a href= "#" > Service 2</a></li>
<li><a href= "#" > Service 3</a></li>
<li><a href= "#" > Service 4</a></li>
</ul>
</li>
<li class= "Topmenu" ><a href= "#" > Success Stories </a>
<ul class= "submenu" >
<li><a href= "#" > Case 1</a></li>
<li><a href= "#" > Case 2</a></li>
<li><a href= "#" > Case 3</a></li>
<li><a href= "#" > Case 4</a></li>
</ul>
</li>
<li class= "Topmenu" ><a href= "#" > About Us </a>
<ul class= "submenu" >
<li><a href= "#" > We 1</a></li>
<li><a href= "#" > We 2</a></li>
<li><a href= "#" > We 3</a></li>
<li><a href= "#" > We 4</a></li>
</ul>
</li>
<li class= "Topmenu" ><a href= "#" > Contact Us </a>
<ul class= "submenu" >
<li><a href= "#" > Contact 1</a></li>
<li><a href= "#" > Contact 2</a></li>
<li><a href= "#" > Contact 3</a></li>
<li><a href= "#" > Contact 4</a></li>
<li><a href= "#" > Contact 5</a></li>
<li><a href= "#" > Contact 6</a></li>
<li><a href= "#" > Contact 7</a></li>
</ul>
</li>

</ul>
</body>


Note that both Getelementsbyclassname and getElementsByTagName have s;

JS can not be used int a=1, to use the Var a=1;

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.