window.onload = function () {
var lis = document.getElementsByTagName('li');
lis[0].style.backgroundColor = 'gold';
lis[1].style.backgroundColor = 'gold';
}
document.getElementsByTagName();標簽獲取元素
循環(huán)
1.for循環(huán)
window.onload = function () {
var olist = document.getElementById('list01');
var ali = olist.getElementsByTagName('li');
for(var i=0;i<8;i++){
if(i%2 == 0){
ali[i].style.backgroundColor = 'gold';
}
}
i++;
}
2.while循環(huán)
var i=0;
while (i<ali.length){
if(i%2 == 0){
ali[i].style.backgroundColor = 'gold';
}
}
i++;
};
3.do while循環(huán)
var i = 0;
do {
if (i % 2 == 0) {
ali[i].style.backgroundColor = 'gold';
}
i++;
} while (i < ali.length);
}
while和do while的區(qū)別:
do while是大括號里無論如何都先執(zhí)行一次峻村,執(zhí)行完再判斷是否成立
while是先判斷是否成立缎患,然后在執(zhí)行大括號的內(nèi)容