1、 splice()方法删除/添加数组中指定位置的内容:
splice(指定的数组下标,删除的个数,替换内容1,替换内容2…);
返回值为数组,内容为被删除的内容
删除指定位置内容:
var arr=["I", "am", "fine", ",", "thank", "you"];
console.log(arr); // 输出["I", "am", "fine", ",", "thank", "you"]
var newArr=arr.splice(3,1);
console.log(newArr); // 输出[","]
console.log(arr); // 输出["I", "am", "fine", "thank", "you"]
替换指定位置内容:
var arr=["I", "am", "fine", ",", "thank", "you"];
console.log(arr); // 输出["I", "am", "fine", ",", "thank", "you"]
var newArr=arr.splice(3,3,"thanks");
console.log(newArr); // 输出[",", "thank", "you"]
console.log(arr); // 输出["I", "am", "fine","thanks"]
在指定位置添加指定内容:若删除个数为0,则不删除任何内容,若同时还有替换内容,则将该内容加在指定下标处:
var arr=["I", "am", "fine", ",", "thank", "you"];
console.log(arr); // 输出["I", "am", "fine", ",", "thank", "you"]
var newArr=arr.splice(6,0,"and","you");
console.log(newArr); // 输出[]
console.log(arr); // 输出["I", "am", "fine", ",", "thank", "you", "and", "you"]
2、iframe标签自适应屏幕大小
var frame=document.getElementById("iframe标签的id");
frame.style.height=document.documentElement.clientHeight+"px";
window.onresize=function(){
frame.style.height=document.documentElement.clientHeight+"px";
};
注:body一定要margin: 0; padding: 0;
3、解决冒泡问题
在子元素触发后执行的代码最后加上return false;
或为子元素绑定阻值冒泡事件:
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
return false;会阻值默认动作,如a标签的跳转,而后者不会。