這篇文章將為大家詳細講解有關常用的js方法有哪些,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
數組及對象深拷貝
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); })()
關于“常用的js方法有哪些”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。