JS gets element instance summary through class

Source: Internet
Author: User
Tags tagname

Baidu-web today to see the front-end development Engineer-Pen test questions, found a long-term neglect of their own problems.
Some colleagues used the code I wrote to get the elements, and the results were not taken. Reason, I believe because she wrote "-" in the class name, which in the regular will be treated as a meta character. So the code can be as written on that front end:

The code is as follows Copy Code

var tea = (function () {
function Getelementsbyclassname (classname,tag,parent) {
". All" is used in the DHTML www.111cn.net, and when tag equals "*",
If the browser supports ". All", otherwise, use the standard Dom method
var alltags= (Tag = = "*" && parent.all)? Parent.all:parent.getElementsByTagName (tag);
var matchingelements= new Array ();
Because '-' is the meta character in the regular expression, and the ClassName '-' is legal,
So, before you write className to a regular expression, you first escape the '-'
Classname=classname.replace (/-/g, "\-");
var regex=new RegExp ("(^|\b)" +classname+ "(\b|$)");
var element;
for (Var i=0;i<alltags.length;i++) {
Element=alltags[i];
if (Regex.test (Element.classname)) {
Matchingelements.push (Element);
}
}
return matchingelements;
}
return {
Getelementsbyclassname:getelementsbyclassname
};
})();

Group of 99 Brothers, told younger brother said, now generally not popular such a matching way, you can use the following ways:

The code is as follows Copy Code


(' +classname.tolowercase () + '). IndexOf (' + class+ to be judged ')!==-1

Example 2

The code is as follows Copy Code


<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= "#" > This is all about the product </a>
<ul class= "submenu" >
<li><a href= "#" > here are all products 1</a></li>
<li><a href= "#" > here are all products 2</a></li>
<li><a href= "#" > here are all products 3</a></li>
<li><a href= "#" > here are all products 4</a></li>
<li><a href= "#" > here are all products 5</a></li>
<li><a href= "#" > here are all 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 we'll find you </a>
<ul class= "submenu" >
<li><a href= "#" > We'll find you 1</a></li>.
<li><a href= "#" > We'll find you 2</a></li>.
<li><a href= "#" > We'll find you 3</a></li>.
<li><a href= "#" > We'll find you 4</a></li>.
</ul>
</li>
<li class= "Topmenu" ><a href= "#" > will contact us. We'll find you. </a>
<ul class= "submenu" >
<li><a href= "#" > here will contact us and we'll find you 1</a></li>
<li><a href= "#" > here will contact us and we'll find you 2</a></li>
<li><a href= "#" > here will contact us and we'll find you 3</a></li>
<li><a href= "#" > here will contact us and we'll find you 4</a></li>
<li><a href= "#" > here will contact us and we'll find you 5</a></li>
<li><a href= "#" > here will contact us and we'll find you 6</a></li>
<li><a href= "#" > here will contact us and we'll find you 7</a></li>
</ul>
</li>
</ul>
</body>

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.