splice()删除/添加数组中指定位置内容 + iframe标签内容自适应屏幕大小 + 解决冒泡现象

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标签的跳转,而后者不会。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值