Home > Article > Web Front-end > Share a collection of commonly used js methods
This article mainly shares knowledge about commonly used js method collections. Has very good reference value. Let’s take a look with the editor below
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);
Get address bar parameters
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());
Modify WeChat title compatible with 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); }
Mobile responsive style
/* 方法使用后会在 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); })()
The above is the detailed content of Share a collection of commonly used js methods. For more information, please follow other related articles on the PHP Chinese website!