您的位置:首页 > 其它

第十二周问题总结——splice()移除/添加数组中指定位置内容,iframe标签内容自适应屏幕大小,冒泡现象

2019-05-19 22:01 253 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/Zoey_J/article/details/90348750

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐