常用的js方法有哪些

发布时间:2021-08-18 14:00:52 作者:小新
来源:亿速云 阅读:96

这篇文章将为大家详细讲解有关常用的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方法有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. JS常用的数组方法有哪些
  2. js常用方法有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js

上一篇:mysql怎么实现ssl主从安全连接

下一篇:ES6中数组array新增方法的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》