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){
ali[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';
}
++;
} while (i < ali.length);
}
while和do while的区别:
do while是大括号里无论如何都先执行一次,执行完再判断是否成立
while是先判断是否成立,然后在执行大括号的内容
网友评论