JS did not get the class method, found some packaging good methods, here to tidy up
(1) Encapsulation first
Encapsulates
the GetClass function getclass (tagname,classname)//obtain a label named TagName, the element
{
if ( Document.getelementsbyclassname)//support this function
{return document.getelementsbyclassname (className);
}
else
{ var tags=document.getelementsbytagname (tagName);//Get tag
var tagarr=[];//to return the element with a class name of 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;
}
(2) Main program
<ul>
<li class= "Dictap" >1</li>
<li class= "Dictap" >2</li>
<li class= " Dictap ">3</li>
<li class=" Dictap ">4</li>
</ul>
(3) Get all Li content program with class as Dictap
Window.onload = function ()
{ var topmenus = getclass (' li ', ' Dictap ');
for (Var i=0;i < topmenus.length i++)
{
alert (topmenus[i].innerhtml);
}
}
(4) Practical application in Project
Click on the custom attribute PersonID, open the Details page and carry the value across pages var Dictap = getclass (' li ', ' Dictap '); for (var i=0;i <dicTap.length; i++) {Dictap[i].addeventlistener (' tap ', function () {var
Personid=this.getattribute ("PersonId");
Localstorage.a=personid;
Mui.openwindow ({url: ' disciplinedetail.html ', id: ' Disciplinedetail '});
}//Package GetClass function GetClass (tagname,classname)//Get tag name TagName, class name ClassName element {
if (document.getelementsbyclassname)//support this function {return document.getelementsbyclassname (className); else {var tags=document.getelementsbytagname (tagName);//Get tag var tagarr=[];//used to return
Returns the element for 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; }
}
Above this JavaScript to get the class of simple implementation is a small series to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.