一、两种遍历方式
for循环
var arr = [1,2,3,4,5]
for(var i=0;i<arr.length;i++){
console.log(arr[i])
}
for in
/*for in
key表示下标值
* */
for(key in arr){
console.log(arr[key])
}
二、充实文档的内容
1.onload事件(addLoadEvent)
function addLoadEvent(func){
/*1.把window.onload事件的处理函数存入变量oldonload*/
/*2.如果window.onload没有绑定任何函数,则给它添加新的函数
*3.如果在window.onload上已经绑定了函数,就把新函数追加到末尾。
* */
var oldonload = window.onload;
if(typeof oldonload != "function"){
window.onload = func;
}else{
window.onload = function(){
oldonload();
func();
}
}
}
2.动画 moveElement函数
function moveElement(elemenID,final_x,final_y,interval){
//移动元素
if(!document.getElementById)return false;
if(!document.getElementById(elemenID))return false;
var elem = document.getElementById(elemenID);
if(elem.movement){
clearTimeout(elem.movement);
}
if(!elem.style.left){
elem.style.left = "0px";
}
if(!elem.style.top){
elem.style.top = "0px";
}
var xpos = parseInt(elem.style.left);
var ypos = parseInt(elem.style.top);
var dist = 0;
if(xpos == final_x && ypos == final_y){
return true;
}
if(xpos < final_x){
//xpos++;
dist = Math.ceil((final_x - xpos)/10);
xpos = xpos + dist;
}
if(xpos > final_x){
//xpos--;
dist = Math.ceil((xpos - final_x)/10);
xpos = xpos - dist;
}
if(ypos < final_y){
//ypos++;
dist = Math.ceil((final_y - ypos)/10);
ypos = ypos + dist;
}
if(ypos > final_y){
//ypos--;
dist = Math.ceil((ypos - final_y)/10);
ypos = ypos - dist;
}
elem.style.left = xpos + "px";
elem.style.top = ypos + "px";
var repeat = "moveElement('"+elemenID+"',"+final_x+","+final_y+","+interval+")";
elem.movement = setTimeout(repeat,interval);
}