当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 常用的js方法合集

常用的js方法合集

2019年03月22日  | 移动技术网IT编程  | 我要评论

数组及对象深拷贝

var arr = [1,'2',{a:1,b:[1,2]}];
function deepcopy(p, c) {    
 var c = c || {};    
 for (var i in p) {      
 if (typeof p[i] === 'object' && p[i] !== null) {  c[i] = (p[i].constructor === array) ? [] : {};    deepcopy(p[i], c[i]);      
 } else {         
  c[i] = p[i];      
 }    
 }    
 return c;  
}
var carr = deepcopy(arr);
console.log(carr);

获取地址栏参数

function geturlparam(){
 var _arr = location.search.substr(1).split('&');
 var _obj = {};
 for (var i = 0; i < _arr.length; i++) {
 _obj[_arr[i].split('=')[0]] = _arr[i].split('=')[1]
 };
 return _obj;
}
console.log(geturlparam());

修改微信title 兼容ios

function changewxtitle(text){
 var $body = $('body');
 document.title = text;
 var $iframe = $('<iframe src="/favicon.ico"></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
 *   })
 *  }
 */
(function pageresize(opt) {
  var ua = navigator.useragent,
    wp = ua.match(/windows phone ([\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;
    // 核心代码:页面缩放比例
    var sx = dw/pw,
      sy = dh/ph; 
    var css = '.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:left top;transform-origin:left top;}',
    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); 
})()

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网