第二部分1.3遍历dom

最后一个任务有意思。

  书上看来以为自己会了是假的,做题实践用出来才是正途。

        first last 快捷方式,要是批量处理还是用下表法通用。
        体会到循环的诞生原因,就是因为重复的东西需要写好多遍。
        不知道为啥,firefox会自动在html文档中的table下添加tbody,如果你没写的话。

/*
    document.body.firstElementChild.firstElementChild.firstElementChild.firstElementChild.style.backgroundColor = "red";
    document.body.firstElementChild.firstElementChild.children[1].children[1].style.background = "red";
    document.body.firstElementChild.firstElementChild.children[2].children[2].style.background = "red";
    document.body.firstElementChild.firstElementChild.children[3].children[3].style.background = "red";
    document.body.firstElementChild.firstElementChild.children[4].children[4].style.background = "red";
    */
    
    /*
        first last 快捷方式,要是批量处理还是用下表法通用。
        体会到循环的诞生原因,就是因为重复的东西需要写好多遍。
        不知道为啥,firefox会自动在html文档中的table下添加tbody,如果你没写的话。
    */
    /*
    //while循环版本
    let i = 0;
    while (i < 5) {
        document.body.children[0].children[0].children[i].children[i].style.background = "red";
        i++;
    }
    */
    
    //for循环版本
    for (let i = 0; i < 5; i++) {
        document.body.children[0].children[0].children[i].children[i].style.background = "red";    
    }

原文地址:https://www.cnblogs.com/JuniorProgramer/p/14897419.html