通过标签获取元素
document.getElementsByTagName();标签获取元素
循环
有规律重复可以用循环来做
for循环
for(var i=0; i<8; i++){
aLi[i].style.background='gold'
}
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. background='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是先判断是否成立,然后在执行大括号的内容
数组去重
var aRr=[1,3,4,1,6,9,1,2,5,3,1,6,5,4,4];
var aRr2=[];
for(var i=0;i<aRr.length;i++){
if (aRr.indexof(aRr[1])==1{
aRr2.push(aRr[i]);
}
}
alert(aRr2);
字符串处理
var str="2018-12-03";
var arr=str.split('-');//2018,12,03
alert(arr);
var arr = str.split('')//"2","0","1","8","-","1","2","-","0","3"
console.log(arr);
touppercase()转大写
tolowercase()转小写
字符串反转
var str = "12sdadsad5646sadsadas";
//spilt()字符串转为数组
//reverse()数组反转
//join()数组转成字符串
var str1 = str.split('').reverse().join('')
alert(str1)
先用spilt方法将字符串转成数组,然后在用数组反转,最后用join将数组转成字符串