window.onload = function(){
var lis = docment.getElementsByTagName("li");
lis[0].style.backgroundColor = "gold";
lis[1].style.backgroundColor = "gold";
}
document.getElementsByTagName(); 标签获取元素
循环
for循环
window.onload = function(){
var olist = document.getElementById("list01");
var ali = olist.getElementsByTagName("li");
for(var i = 0; i <8; i++){
if (i%2 == 0){
all[i].style.backgroundColor = "gold";
}
}
i++;
}
while循环
var i = 0;
while (i< ali.length){
if(i%2 == 0){
ali[i].style.backgroundColor = "gold";
}
i++;
};
do while循环
var i = 0;
do{
if (i %2==0){
ali[i].style.backgroundColor = "gold";
}
i++;
}while (i < ali.length);
}
while和do while 的区别:
do while 是大括号里无论如何都先执行一次,执行完再判断是否成立
while是先判断是否成立,然后在执行大括号的内容
网友评论