常用的js方法合集
数组及对象深拷贝
vararr=[1,'2',{a:1,b:[1,2]}];
functiondeepCopy(p,c){
varc=c||{};
for(variinp){
if(typeofp[i]==='object'&&p[i]!==null){c[i]=(p[i].constructor===Array)?[]:{};deepCopy(p[i],c[i]);
}else{
c[i]=p[i];
}
}
returnc;
}
varcArr=deepCopy(arr);
console.log(cArr);
获取地址栏参数
functiongetUrlParam(){
var_arr=location.search.substr(1).split('&');
var_obj={};
for(vari=0;i<_arr.length;i++){
_obj[_arr[i].split('=')[0]]=_arr[i].split('=')[1]
};
return_obj;
}
console.log(getUrlParam());
修改微信title兼容ios
functionchangeWxTitle(text){
var$body=$('body');
document.title=text;
var$iframe=$('');
$iframe.on('load',function(){
setTimeout(function(){
$iframe.off('load').remove();
},0);
}).appendTo($body);
}
移动端响应式样式
/*方法使用后会在head标签添加一个style标签并且有.my-resize和.no-resize的样式,需要适配屏幕的元素加上.my-resize类名即可,.no-resize是还原已适配的元素
*window.onload=window.onresize=function(){
*pageResize({
*width:'320',//默认宽320px
*height:'504',//默认高504px
*})
*}
*/
(functionpageResize(opt){
varua=navigator.userAgent,
wp=ua.match(/WindowsPhone([\d.]+)/),
android=ua.match(/(Android);?[\s\/]+([\d.]+)?/),
//设备宽高初始比例
dw=document.documentElement.clientWidth,
dh=document.documentElement.clientHeight,
ds=dw/dh,
//页面宽高初始比例
opt=opt||{},
pw=opt.width||320,
ph=opt.height||512,
ps=pw/ph;
//核心代码:页面缩放比例
varsx=dw/pw,
sy=dh/ph;
varcss='.no-resize{-webkit-transform:scaleY('+sx/sy+');transform:scaleY('+sx/sy+');}.my-resize{width:'+pw+'px!important;height:'+ph+'px!important;-webkit-transform:scale('+sx+','+sy+');transform:scale('+sx+','+sy+');-webkit-transform-origin:lefttop;transform-origin:lefttop;}',
head=document.getElementsByTagName('head')[0],
style=document.createElement('style');
style.type='text/css';
if(style.styleSheet){
style.styleSheet.cssText=css;
}else{
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
})()
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!