window.onload = function () {
var lis = document.getElementsByTagName('li');
lis[0].style.backgroundColor = 'gold';
lis[1].style.backgroundColor = 'gold';
}
document.getElementsByTagName();标签获取元素
循环
1.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++;
}
2.while循环
var i=0;
while (i<ali.length){
if(i%2 == 0){
ali[i].style.backgroundColor = 'gold';
}
}
i++;
};
3.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是先判断是否成立,然后在执行大括号的内容