练习,多个相同元素根据元素的html值来设置不同的class
<style> .s1{color: rebeccapurple;font-size: 25px;} .s2{color: red;} .s3{color: blue;} .s4{color:yellow;} </style> <div id="div"> <span>1</span> <span>2</span> <span>1</span> <span>3</span> <span>1</span> </div> <script> var div=document.getElementById('div'); var child=div.children; var ar=[]; for(i=0;i<child.length;i++){ var n=child[i].innerHTML; ar.push(n); } for(i=0;i<ar.length;i++){ child[i].className='s'+ar[i]; } </script>