常用js方法
function dateGetter(name, size, offset, trim) {
offset = offset || 0;
return function (date) {
var value = date['get' + name]();
if (offset > 0 || value > -offset)
value += offset;
if (value === 0 && offset == -12) value = 12;
return padNumber(value, size, trim);
};
};
function padNumber(num, digits, trim) {
var neg = '';
if (num < 0) {
neg = '-';
num = -num;
}
num = '' + num;
while (num.length < digits) num = '0' + num;
if (trim)
num = num.substr(num.length - digits);
return neg + num;
};
function dateStrGetter(name, shortForm) {
return function(date, formats) {
var value = date['get' + name]();
var get = (shortForm ? ('SHORT' + name) : name).toUpperCase();
return formats[get][value];
};
};
window.IGrow = {};
var Utilities = {
getParameterByName: function (name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
},
findElement: function (arr, propName, propValue) {
for (var i = 0; i < arr.length; i++)
if (arr[i][propName] == propValue)
return arr[i];
},
findWithAttr: function (array, attr, value) {
for (var i = 0; i < array.length; i += 1) {
if (array[i][attr] === value) {
return i;
}
}
},
jsonObjToBase64: function (json) {
return btoa(encodeURIComponent(JSON.stringify(json)));
},
base64TojsonObj: function (base64) {
return JSON.parse(decodeURIComponent(atob(base64)));
},
pageJump: function (url) {
location.href = url;
},
log: function () {
for(key in arguments){
console.log(JSON.parse(JSON.stringify(arguments[key])));
}
},
copy: function (source) {
var result = source instanceof Array ? [] : {};
for (var key in source) {
result[key] = typeof source[key] === 'object' ? this.copy(source[key]) : source[key];
}
return result;
},
params: function () {
var url = window.location.search;
if (url.indexOf("?") != -1) {
var str = url.substr(1),
strs = str.split("&"),
key = new Array(strs.length),
value = new Array(strs.length),
params = {};
for (var i = 0; i < strs.length; i++) {
key[i] = strs[i].split("=")[0]
value[i] = unescape(strs[i].split("=")[1]);
params[key[i]] = value[i]
}
return params;
}
},
getTime: function (date, format) {
var text = '',
parts = [],
fn, match;
while (format) {
match = /((?:[^yMdHhmsaZEwG']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+|H+|h+|m+|s+|a|Z|G+|w+))(.*)/.exec(format);
if (match) {
parts = parts.concat([].slice.call(match, 1));
format = parts.pop();
} else {
parts.push(format);
format = null;
}
}
parts.forEach(function(value) {
fn = DATE_FORMATS[value];
text += fn ? fn(new Date(date))
: value.replace(/(^'|'$)/g, '').replace(/''/g, "'");
});
return text;
},
tip: function (msg,time) {
var html = '<div class="weui_dialog_alert" id="tip">' +
'<div class="weui_mask"></div>' +
'<div class="weui_dialog">' +
'<div class="weui_dialog_hd"><strong class="weui_dialog_title">提示</strong></div>' +
'<div class="weui_dialog_bd">' + msg + '</div>' +
'<div class="weui_dialog_ft">', time = time || 2000;
if ($('#tip').length) {
$('#tip').find('.weui_dialog_bd').html(msg);
$('#tip').show();
} else {
$('body').append(html);
}
$('#tip').off().click(function(){
$(this).hide();
});
setTimeout(function () {
$('#tip').hide();
}, time);
},
extend: function () {
var _extend,
_isObject,
arr = arguments,
result = {},
i;
_isObject = function (o) {
return Object.prototype.toString.call(o) === '[object Object]';
};
_extend = function self(destination, source) {
var property;
for (property in destination) {
if (destination.hasOwnProperty(property)) {
// 若destination[property]和sourc[property]都是对象,则递归
if (_isObject(destination[property]) && _isObject(source[property])) {
self(destination[property], source[property]);
}
;
// 若sourc[property]已存在,则跳过
if (source.hasOwnProperty(property)) {
continue;
} else {
source[property] = destination[property];
}
}
}
};
if (!arr.length) return {};
for (i = arr.length - 1; i >= 0; i--) {
if (_isObject(arr[i])) {
_extend(arr[i], result);
}
}
arr[0] = result;
return result;
},
forEach: function (obj, iterator, context) {
var key, length;
if (obj) {
if (typeof obj == 'function') {
for (key in obj) {
// Need to check if hasOwnProperty exists,
// as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function
if (key != 'prototype' && key != 'length' && key != 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
iterator.call(context, obj[key], key, obj);
}
}
} else if ($.isArray(obj)) {
var isPrimitive = typeof obj !== 'object';
for (key = 0, length = obj.length; key < length; key++) {
if (isPrimitive || key in obj) {
iterator.call(context, obj[key], key, obj);
}
}
} else if (obj.forEach && obj.forEach !== Utilities.forEach) {
obj.forEach(iterator, context, obj);
} else {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
iterator.call(context, obj[key], key, obj);
}
}
}
}
return obj;
}
};
Utilities.routeParams = Utilities.params();
常用js方法,extend合并对象,copy深拷贝对象,tip提示信息框,getTime返回自定义格式时间,params获取浏览器地址参数,log打印相关变量,forEach循环对象或数组
常用js方法的更多相关文章
- 常用js方法整理common.js
项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...
- 项目中常用js方法整理common.js
抽空把项目中常用js方法整理成了common.js,都是网上搜集而来的,大家一起分享吧. var h = {}; h.get = function (url, data, ok, error) { $ ...
- 常用js方法封装
常用js方法封装 var myJs = { /* * 格式化日期 * @param dt 日期对象 * @returns {string} 返回值是格式化的字符串日期 */ getDates: fun ...
- 常用js方法整理(个人)
开头总要有点废话 今天想了下,还是分享下自己平时积累的一些实用性较高的js方法,供大家指点和评价.本想分篇介绍,发现有点画蛇添足.整理了下也没多少拿得出手的方法,自然有一些是网上看到的个人觉得很有实用 ...
- Dynamics CRM 常用 JS 方法集合
JS部分 拿到字段的值 var value= Xrm.Page.getAttribute("attributename").getValue(); Xrm.Page.getAttr ...
- 常用JS方法整理
目录: 截取指定字节数的字符串 判断是否微信 获取时间格式的几个举例 获取字符串字节长度 对象克隆.深拷贝 组织结构代码证验证 身份证号验证 js正则为url添加http标识 URL有效性校验方法 自 ...
- 前端开发常用 JS 方法
1,获取文件本地url,在上传之前预览 /** * 获取图片嗯滴url,在上传之前预览 * @param file 选择的图片文件 * @returns {*} url */ getFileLocat ...
- 项目常用JS方法封装--奋斗的IT青年(微信公众号)
...
- 常用js方法函数
常用方法函数 1.深复制 // 1.深复制 function deepCopy(source) { var result = {}; for (var key in source) { result[ ...
随机推荐
- Leetcode: Validate IP Address
In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...
- Timusoj 1982. Electrification Plan
http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 sec ...
- linux系统无法启动解决方案
windows和linux双系统一般先安装Windows,分两个主分区,把Linux安装在另外的主分区上.Linux编译内核,添加NTFS分区支持,然后Mount NTFS 分区即可访问Windows ...
- NDK开发-零散知识点整理
JavaVM 标准Java平台下,每一个Process可以产生很多JavaVM对象,但在Android平台上,每一个Process只能产生一个Dalvik VM对象,也就是说在Android进程中是通 ...
- java_Excel 导出
package Demo; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; i ...
- 利用selector设置ImageButton不同状态下的背景图片
1.自定义MyButton类 public class MyButton extends Button { //This constructormust be public MyButton(Cont ...
- 在windows通过visual studio远程调试linux mono程序
本文参考文章 https://github.com/techl/MonoRemoteDebugger 1.通过连接https://github.com/techl/MonoRemoteDebugger ...
- 微信聊天记录查看器(程序+源码) - iOS版
本文版权归cxun所有,如有转载请注明出处与本文链接,谢谢!原文地址:http://www.cnblogs.com/cxun/p/4338643.html Updates [2016.10.14]感谢 ...
- Array类
class Array Arrays are ordered, integer-indexed collections of any object. Array indexing starts at ...
- linux关闭双显卡的方法
我正在使用的一体机是双显卡,在linux下没有很么太好的办法来切换双显卡, 导致使用一会儿后就发烫,关键是这时风扇呜呜的响很吵人 最后找到了下面的解决方法来关掉独立显卡 见 https://githu ...