前端工作中,常会用到一些js脚本,可能会想不起来怎么写,但是一看就知道,所以在此做下记录:
1. 阻止iframe内的js
在iframe标签中添加属性:security="restricted" sandbox=""
<iframe security="restricted" sandbox="" class="ijc" width="100%" height="100%" frameborder="0" marginwidth="0" scrolling="no" src="w.html"></iframe>
2. jquery
$("tr:odd") -- 选择奇数行
$("tr:even") -- 选择偶数行
3. iframe子页面跳转
var location = new Object();
if (top.location != location)
top.location.href = location.href;
4. iframe 子页面调用父页面函数:
window.parent.test();
5. iframe父页面调用子页面函数:
document.getElementById("iframe-jc").contentWindow.test();
6. 判断是否IE浏览器:
var ie = !-[1,];
alert(ie);
7. 删除数组中的元素:
var jSize=this.jspArr.length;
for(var i=0;i<jSize;i++){
if(this.jspArr[i].id==idx){
this.jspArr.splice(i,1);
}
}
8. jquery获取父页面中的元素:
$("#iframe_my-work",window.parent.document).css("height",ht+"px");
iframe_my-work为父页面中的元素。
9.兄弟元素
$.siblings()
10. Math
Math.round(exp1); //四舍五入
Math.floor(rslt); //返回小于等于原rslt的最大整数。
Math.ceil(rslt); //返回大于等于原rslt的最小整数。
11. 时间
var date=new Date();
var str=date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
str+=' '+date.getHours()+':'+(date.getMinutes()-2)+':'+date.getSeconds();
alert(str);
12. json转为数组
eval('(' + data + ')');
13. //去掉所有的html标记
function nohtml(str){
return str.replace(/<[^>]+>/g,"");
}
14. 去除空格
str.replace(/(^\s*)|(\s*$)/g, "");
15. 去掉换行符
content=content.replace(/[\n]/ig,'');
16.
<script>
var sup=window.parent.location;
var sub=window.location.href;
if(sup!=sub){
window.parent.location=sub;
}
</script>
17.
判断是否为数字(包括小数)
if(!isNaN(val)){
return true;
}else{
return false;
}
18.
判断是否为整数
function checkInt(){
if(val==0){
return true;
}
var regPos=/^[1-9]+[0-9]*]*$/;
if(regPos.test(val)){
return true;
}
return false;
}