练习,多个相同元素根据元素的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>
版权声明:本文为no000000原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。