JQuery to achieve interlaced background color change

  • 2020-03-30 04:23:43
  • OfStack

Interlacing color change there are many ways to achieve, before I will be directly to judge the code written in the program, today we will use jQuery implementation, the code is delivered ~

JQuery code:


       $(function(){
            var item = $("tr");
            for(var i=0;i<item.length;i++){
                if(i%2==0){
                    item[i].style.backgroundColor="#888";
                }
            }
            //$("#tb tbody tr:even").css("backgroundColor","#888");
 
        });

HTML code:


    <table id='tb'>
        <tbody>
            <tr><td> The first line </td><td> The first line </td></tr>
            <tr><td> The second line </td><td> The first 2 line </td></tr>
            <tr><td> The third line </td><td> The first 3 line </td></tr>
            <tr><td> The first 4 line </td><td> The first 4 line </td></tr>
            <tr><td> The first 5 line </td><td> The first 5 line </td></tr>
            <tr><td> The first 6 line </td><td> The first 6 line </td></tr>
        </tbody>
    </table>

Is not very fun, friends can actually use jQuery to achieve a lot of fun things, you are free to play


Related articles: