常用的js方法合集
更新时间:2017年03月10日 11:47:59 作者:fe_kevin
本文主要介绍了常用的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);
})()
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
相关文章
微信小程序实现Session功能及无法获取session问题的解决方法
这篇文章主要介绍了微信小程序实现Session功能及无法获取session问题的解决方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2019-05-05
JavaScript实战(原生range和自定义特效)简单实例
下面小编就为大家带来一篇JavaScript实战(原生range和自定义特效)简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-08-08
js getBoundingClientRect使用方法详解
这篇文章主要介绍了js getBoundingClientRect使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-07-07
layui checkbox默认选中,获取选中值,清空所有选中项的例子
今天小编就为大家分享一篇layui checkbox默认选中,获取选中值,清空所有选中项的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2019-09-09
JavaScript获取鼠标移动时的坐标(兼容IE8、chome谷歌、Firefox)
这篇文章主要介绍了JavaScript获取鼠标移动时的坐标(兼容IE8、chome谷歌、Firefox浏览器),需要的朋友可以参考下2014-09-09


最新评论