Construction of html table, traversal to change its background color

// Change the color of even-numbered rows in the table, the operation method.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<-How the table is defined->
<table id="tb">
    <tbody>
    <tr><td>first line</td><td>first line</td></tr>
    <tr><td>Second line</td><td>Second line</td></tr>
    <tr><td>third line</td><td>third line</td></tr>
    <tr><td>fourth line</td><td>fourth line</td></tr>
    </tbody>
</table>
<script>
    window.onload=function(){
        var item= document.getElementById("tb")
        var tbody=item.getElementsByTagName("tbody")[0]
        var trs= tbody.getElementsByTagName("tr")
        alert(trs.length)
        for(var i=0;i<trs.length;i++){
            if(i%2==0){
                trs[i].style.background="#888"
            }
        }
    }
</script>
</body>
</html>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326096096&siteId=291194637