>  기사  >  웹 프론트엔드  >  일반적으로 사용되는 js 메소드 모음 공유

일반적으로 사용되는 js 메소드 모음 공유

高洛峰
高洛峰원래의
2017-03-12 14:09:271156검색

이 글은 주로 일반적으로 사용되는 js 메소드 컬렉션에 대한 지식을 공유합니다. 매우 좋은 참조 값을 가지고 있습니다. 아래 에디터를 살펴보겠습니다

ArrayObjectDeep copy

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(&#39;=&#39;)[0]] = _arr[i].split(&#39;=&#39;)[1]
 };
 return _obj;
}
console.log(getUrlParam());

iOS와 호환되는 WeChat 제목 수정

function changeWxTitle(text){
 var $body = $(&#39;body&#39;);
 document.title = text;
 var $iframe = $(&#39;<iframe src="/favicon.ico"></iframe>&#39;);
 $iframe.on(&#39;load&#39;,function() {
 setTimeout(function() {
  $iframe.off(&#39;load&#39;).remove();
 }, 0);
 }).appendTo($body);
}

모바일 반응형

/* 方法使用后会在 head标签添加一个style标签 并且有.my-resize 和 .no-resize的样式,需要适配屏幕的元素加上.my-resize类名即可,.no-resize是还原已适配的元素
 * window.onload = window.onresize = function(){
 *   pageResize({
 *     width : &#39;320&#39;,   //默认宽320px 
 *     height : &#39;504&#39;,   //默认高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 = &#39;.no-resize { -webkit-transform: scaleY(&#39;+sx/sy+&#39;);transform: scaleY(&#39;+sx/sy+&#39;); }.my-resize { width:&#39;+pw+&#39;px !important;height:&#39;+ph+&#39;px !important;-webkit-transform: scale(&#39;+sx+&#39;,&#39;+sy+&#39;);transform: scale(&#39;+sx+&#39;,&#39;+sy+&#39;); -webkit-transform-origin:left top;transform-origin:left top;}&#39;,
    head = document.getElementsByTagName(&#39;head&#39;)[0],
    style = document.createElement(&#39;style&#39;);
    style.type = &#39;text/css&#39;;
    if(style.styleSheet){
      style.styleSheet.cssText = css;
    }else{
      style.appendChild(document.createTextNode(css));
    }
    head.appendChild(style); 
})()

위 내용은 일반적으로 사용되는 js 메소드 모음 공유의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.