常用一些方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//浏览器URL中查询字符串中的参数
//http://www.runoob.com/jquery/misc-trim.html?channelid=12333&name=xiaoming&age=23
function showWindowHref(){
var sHref = window.location.href;
var args = sHref.split('?');
if(args[0] == sHref){
return "";
}
var arr = args[1].split('&');
var obj = {};
for(var i = 0;i< arr.length;i++){
var arg = arr[i].split('=');
obj[arg[0]] = arg[1];
}
return obj;
}
var href = showWindowHref(); // obj
console.log(href['name']); // xiaoming
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//时间处理
FormatDateTime:function(strTime){
if(strTime){
var date = new Date(strTime),
Y = date.getFullYear(),
m = date.getMonth() + 1,
d = date.getDate(),
H = date.getHours(),
i = date.getMinutes(),
s = date.getSeconds();
if (m < 10) {
m = '0' + m;
}
if (d < 10) {
d = '0' + d;
}
if (H < 10) {
H = '0' + H;
}
if (i < 10) {
i = '0' + i;
}
if (s < 10) {
s = '0' + s;
}
// <!-- 获取时间格式 2017-01-03 10:13:48 -->
// var t = Y+'-'+m+'-'+d+' '+H+':'+i+':'+s;
var t = Y+'-'+m+'-'+d;
// <!-- 获取时间格式 2017-01-03 -->
// var t = Y + '-' + m + '-' + d;
return t;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
function quickerFunction(){
this.setCookie=function(objName,objValue,path,objHours){
var str = objName + "=" + encodeURI (objValue);
if(objHours > 0){//为时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date();
var ms = objHours*3600*1000;
date.setTime(date.getTime() + ms);
str += (";expires=" + date.toGMTString());
}
if(path){
str+=((path) ? (";path=" + path) : "");
}
document.cookie = str;
};
this.getCookie=function(name) {
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) {
return decodeURI(arr[2]);
}
return null;
};
this.delCookie=function(name,path){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=this.getCookie(name);
if(cval!=null){
var str = name + "="+'aaa'+";expires="+exp.toGMTString();
if(path){
str+=((path) ? (";path=" + path) : "");
}else{
str+=";path=/";
}
document.cookie= str;
}
};
this.placeholder=function(nodes,pcolor) {
if(!("placeholder" in document.createElement("input"))){
for(i=0;i<nodes.length;i++) {
var self = nodes[i],
placeholder = self.getAttribute('placeholder') || '';
self.onfocus = function () {
if (this.value == this.getAttribute('placeholder')) {
this.value = '';
this.style.color = "";
}
};
self.onblur = function () {
if (this.value == '') {
this.value = this.getAttribute('placeholder');
this.style.color = pcolor;
}
};
self.value = placeholder;
self.style.color = pcolor;
}
}
};
this.dateStringToLongTime=function(datestr,type){
var arr,year,month,dt,hour,minute,s,longTime,date;
if(type!==" ")
{
arr=datestr.split(" ")[0].split(type);
year=arr[0],month=arr[1],dt=arr[2];
hour=datestr.split(" ")[1].split(':')[0];
minute=datestr.split(" ")[1].split(':')[1];
s=datestr.split(" ")[1].split(':')[2];
}
date=new Date(year,parseInt(month)-1,dt,hour,minute,s);
longTime=date.getTime();
return longTime;
};
this.longTimeToDateString=function(longtime,type){
var date=new Date(longtime),m=(date.getMonth()+1)<10?"0"+(date.getMonth()+1):(date.getMonth()+1),
d=date.getDate()<10?"0"+date.getDate():date.getDate(),h=date.getHours()<10?"0"+date.getHours():date.getHours(),
min=date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes(),
s=date.getSeconds()<10?"0"+date.getSeconds():date.getSeconds();
var str=date.getFullYear()+type+m+type+d+" "+h+":"+min+":"+s;
return str;
};
this.dateToString=function (date,type) {
var m=(date.getMonth()+1)<10?"0"+(date.getMonth()+1):(date.getMonth()+1),
d=date.getDate()<10?"0"+date.getDate():date.getDate(),h=date.getHours()<10?"0"+date.getHours():date.getHours(),
min=date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes(),
s=date.getSeconds()<10?"0"+date.getSeconds():date.getSeconds();
var str=date.getFullYear()+type+m+type+d+" "+h+":"+min+":"+s;
return str;
};
this.returnTop=function () {
document.documentElement.scrollTop=0;
document.body.scrollTop=0
};
this.validate=function (v,type) {
// type:1.空值,2.手机,3.email,4.数字,5,校验密码:数字、字母、下划线6-12位
var reg;
switch (type){
case 1:
if(!v)
return false;
else return true;
break;
case 2:
reg=/^1[34578]\d{9}$/;
return reg.test(v);
break;
case 3:
reg=/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
return reg.test(v);
break;
case 4:
reg=/^[0-9]$/;
return reg.test(v);
break;
case 5:
reg=/^[\w*\_*]{6,12}$/;
return reg.test(v);
break;
}
};
this.getParam=function(p){
var url=window.location.href,search=window.location.search,
paramsArr=search.replace("?",'').split('&'),params={},result;
for(var i=0;i<paramsArr.length;i++)
{
if(paramsArr[i].indexOf(p)===0)
{
result=paramsArr[i].split('=')[1];
break;
}
}
return result;
};
this.windowClientSize=function() {
var e = window,
a = 'inner';

if (!('innerWidth' in window )){
a = 'client';
e = document.documentElement || document.body;
}

return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
};

//以下方法都兼容ie
//页面的滚动条是否到达底部
this.isBottom=function (offset) {
//页面高度=滚动条高度+可视区域高度
var clientHeight=this.getClientHeight(),scrollTop=this.getScrollTop(),
pageHeight=this.getScrollHeight();
if(pageHeight+offset<=clientHeight+scrollTop)
{
return true;
}
};
//某个元素的滚动条是否到达底部
this.isBottomForElement=function (offset,element) {
//容器页面高度=容器滚动条高度+容器可视区域高度
var clientHeight=element.offsetHeight,scrollTop=element.scrollTop,
pageHeight=element.scrollHeight;
if(pageHeight+offset<=clientHeight+scrollTop)
{
return true;
}
};
//某个元素是否到达底部
this.isMoveToPos=function (element,offset,type,leaveStop) {
//元素高度=滚动条高度+可视区域高度
var clientHeight=this.getClientHeight(),scrollTop=this.getScrollTop(),elementHeight;
if(type=='top') elementHeight=this.offsetTop(element);
else if(type=="bottom") elementHeight=this.offsetTop(element)+element.offsetHeight;
else console.error("isMoveToPos type is 'top' or 'bottom'");
if(elementHeight+offset<=clientHeight+scrollTop)
{
return true;
}
};
this.getScrollTop=function ()
{
var scrollTop=0;
if(document.documentElement&&document.documentElement.scrollTop)
{
scrollTop=document.documentElement.scrollTop;
}
else if(document.body)
{
scrollTop=document.body.scrollTop;
}
return scrollTop;
};
this.getClientHeight=function ()
{
var clientHeight=0;
if(document.body.clientHeight&&document.documentElement.clientHeight)
{
clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
}
else
{
clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
}
return clientHeight;
};
/********************
* 取文档内容实际高度
*******************/
this.getScrollHeight=function ()
{
return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
};
/*
获取元素离页面顶部距离
*/
this.offsetTop=function ( elements ){
var top = elements.offsetTop;
var parent = elements.offsetParent;
while( parent != null ){
top += parent.offsetTop;
parent = parent.offsetParent;
}
return top;
};
this.offsetLeft=function( elements ){
var left = elements.offsetLeft;
var parent = elements.offsetParent;
while( parent != null ){
left += parent.offsetLeft;
parent = parent.offsetParent;
}
return parent;
};
}
var quicker=new quickerFunction();
感谢你的打赏哦!