I heard it is the most commonly used. . . . I thought of it after watching the dom programming art.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Unt titled document</title> <style> .ca{background-color:red; padding:20px;} .js{ border:1px solid #00F; padding:10px;} </style> </head> <body> <div> sss </div> <div id="as"> </div> <div> </div> <div> </div> <div> </div> <div> </div> </body> </html> <script> function getElementsClass(classnames){ var classobj= new Array();//Define array var classint=0;//Define array subscript var tags=document.getElementsByTagName("*");//Get all tags of HTML for(var i in tags){//Transf the tag if(tags[i].nodeType==1){//Judge the node type if(tags[i].getAttribute("class") == classnames)//Judge the same name as the CLASS required, and form an array { classobj[classint]=tags[i]; classint++; } } } return classobj;//Return the array composed} //The following is the test var a=getElementsClass("ca"); a[0].onclick=function(){alert("We're here");} a[1].innerHTML='We're here'; </script>Sometimes there are multiple dom classes the same, so you can handle it like this:
The code copy is as follows:
var a=getElementsClass("ca");
for(var i=0;i<a.length;i++){
(function(i){
alert(a[i])
})(i)
}
If you have multiple same classes, you can also use them together. If you want to achieve JQ, where can you directly.click still needs to do some more complicated processing.