学无止境,加油屌丝

练习,多个相同元素根据元素的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>

 

posted @ 2020-05-09 10:23  nofeel666  阅读(270)  评论(0编辑  收藏  举报