JavaScript学习总结(11)——JS常用函数(二)
2024-10-20 07:59:27
37. getElementsByClassName
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
function
var
'*' ) var
for
var
if
var
' ); for
var
if
els.push(tags); break } } } } return
} |
38. 获取页面高度
1
2
3
4
5
6
|
function
var
"BackCompat" ? : return
} |
39. 获取页面可视宽度
1
2
3
4
5
6
|
function
var
"BackCompat" ? : return
} |
40. 获取页面宽度
1
2
3
4
5
6
|
function
var
"BackCompat" ? : return
} |
41. 获取页面scrollTop
1
2
3
4
|
function
var
return
} |
42. 获取页面可视高度
1
2
3
4
5
6
|
function
var
"BackCompat" ? : return
} |
43. 去掉url前缀
1
2
3
4
5
6
7
|
function
a=a.replace(/:/g, ":" ).replace(/./g, "." ).replace(///g, "/" ); while (trim(a).toLowerCase().indexOf( "http://" )==0){ a=trim(a.replace(/http:\/\ //i,"")); } return
} |
44. 随机数时间戳
1
2
3
4
|
function
var
return
new
} |
45. 全角半角转换
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
|
//iCase: function
if ( typeof
"string"
return
} var
if (iCase){ /*半->全*/ for (i=0; iCode if (iCode iCode } else
(iCode iCode } oRs.push(String.fromCharCode(iCode)); } } else { /*全->半*/ for (i=0; iCode if (iCode iCode } else
(iCode iCode } oRs.push(String.fromCharCode(iCode)); } } return
"" ); } |
46. 确认是否键盘有效输入值
1
2
3
4
5
6
7
8
9
10
|
function
if (iKey return
;} /*空格和异常*/ if (iKey>47 return
;} /*数字*/ if (iKey>64 return
;} /*字母*/ if (iKey>95 return
;} /*数字键盘1*/ if (iKey>108 return
;} /*数字键盘2*/ if (iKey>185 return
;} /*符号1*/ if (iKey>218 return
;} /*符号2*/ return
; } |
47. 获取网页被卷去的位置
1
2
3
4
5
6
7
8
9
|
function
return
x: y: }: x: y: } } |
48. 日期格式化函数+调用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Date.prototype.format function (format){ var
"M+"
this .getMonth()+1, //month "d+"
this .getDate(), //day "h+"
this .getHours(), //hour "m+"
this .getMinutes(), //minute "s+"
this .getSeconds(), //second "q+"
this .getMonth()+3)/3), //quarter "S"
this .getMilliseconds() //millisecond }; if (/(y+)/.test(format)) ( this .getFullYear()+ "" ).substr(4 for ( var
in
if ( new
"(" + ")" ).test(format)) format "00" + "" + } return
} alert( new
"yyyy-MM-dd )); |
49. 时间个性化输出功能
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
|
/* 1、< 2、>= 3、>= 4、>= 5、>= */ function
var
new
curDate new
year month day hour minute curYear curHour timeStr; if (year timeStr '年' + '月' + '日 + ':' + } else { var
pastH if (pastH timeStr '月' + '日 + ':' + } else
(pastH timeStr '今天
':' + '分' ; } else { var
if (pastM timeStr '分钟前' ; } else { timeStr '刚刚' ; } } } return
} |
50. 解决offsetX兼容性问题
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
|
// function
var
// eventCoord, pageCoord, offsetCoord; // pageCoord // eventCoord X Y }; // offsetCoord X Y }; return
} function
var
// // while
coord.X coord.Y element } return
} |
51. 常用的正则表达式
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
|
//正整数 /^[0-9]*[1-9][0-9]*$/; //负整数 /^-[0-9]*[1-9][0-9]*$/; //正浮点数 /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/; //负浮点数 /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //浮点数 /^(-?\d+)(\.\d+)?$/; //email地址 /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/; //url地址 /^[a-zA-z]+: //(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$/; 或:^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\ ':+!]*([^<>\"\"])*$ //年/月/日(年-月-日、年.月.日) /^(19|20)\d\d[- //匹配中文字符 /[\u4e00-\u9fa5]/; //匹配帐号是否合法(字母开头,允许5-10字节,允许字母数字下划线) /^[a-zA-Z][a-zA-Z0-9_]{4,9}$/; //匹配空白行的正则表达式 /\n\s*\r/; //匹配中国邮政编码 /[1-9]\d{5}(?!\d)/; //匹配身份证 /\d{15}|\d{18}/; //匹配国内电话号码 /(\d{3}-|\d{4}-)?(\d{8}|\d{7})?/; //匹配IP地址 /((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)/; //匹配首尾空白字符的正则表达式 /^\s*|\s*$/; //匹配HTML标记的正则表达式 < //sql ^(select|drop|delete|create|update|insert).*$ //提取信息中的网络链接 (h|H)(r|R)(e|E)(f|F) |")?(\w|\\|\/|\.)+( '|"| //提取信息中的邮件地址 \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* //提取信息中的图片链接 (s|S)(r|R)(c|C) |")?(\w|\\|\/|\.)+('|"| //提取信息中的 (\d+)\.(\d+)\.(\d+)\.(\d+) //取信息中的中国手机号码 (86)*0*13\d{9} //提取信息中的中国邮政编码 [1-9]{1}(\d+){5} //提取信息中的浮点数(即小数) (-?\d*)\.?\d+ //提取信息中的任何数字 (-?\d*)(\.\d+)? //电话区号 ^0\d{2,3}$ //腾讯 ^[1-9]*[1-9][0-9]*$ //帐号(字母开头,允许 ^[a-zA-Z][a-zA-Z0-9_]{4,15}$ //中文、英文、数字及下划线 ^[\u4e00-\u9fa5_a-zA-Z0-9]+$ |
52. 返回顶部的通用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function
var
var
var
window.onscroll btn.style.display "none" ; btn.onclick function () btn.style.display "none" ; window.onscroll null ; this .timer function () d.scrollTop b.scrollTop if
}, }; function
btn.style.display 'block' : "none" } }; backTop( 'goTop' ); |
53. 获得URL中GET参数值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// function
querystr "?" ) if (querystr[1]){ GETs "&" ); GET for (i=0;i<GETs.length;i++){ tmp_arr "=" ) key=tmp_arr[0] GET[key] } } return
} |
54. 打开一个窗体通用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function
var
var
var
"Microsoft ); if
var
"resizable=1,location=no,scrollbars=no,width=" ; p p ",height=" ; p p ",left=" ; p p ",top=" ; p retval } else
var
"ZyiisPopup" , "top="
",left="
",scrollbars="
",dialog=yes,modal=yes,width="
",height="
",resizable=no"
eval( "try ); win.focus(); } } |
55. 提取页面代码中所有网址
1
2
|
var
"\']*([^\"\'\(\)\<\>\[\] \r\n ").replace(/^(src=|href=|url\()[\"\']*|[\"\'\>\) "); alert(aa); |
56. 清除相同的数组
1
2
3
4
5
6
7
8
9
10
|
String.prototype.unique= function (){ var
this .split(/[\r\n]+/); var
'' ; for ( var
if (! new
"^" +x.replace(/([^\w])/ig, "\\$1" )+ "$" , "igm" ).test(y)){ y+=x+ "\r\n" } } return
}; |
57. 按字母排序,对每行进行数组排序
1
2
3
4
5
|
function
var
"\r\n" ); //顺序 var
"\r\n" ); //反序 K1.value=K1.value!=text?text:test; } |
58. 字符串反序
function IsReverse(text){
return text.split('').reverse().join('');
}
59. 清除html代码中的脚本
1
2
3
4
5
6
7
8
9
10
11
|
function
K1.value=K1.value.replace(/<script.*?>[\s\S]*?<\/script>|\s+on[a-zA-Z]{3,16}\s?=\s? "[\s\S]*?" |\s+on[a-zA-Z]{3,16}\s?=\s? '[\s\S]*?' |\s+on[a-zA-Z]{3,16}\s?=[^ "" ); } 动态执行JavaScript脚本 function
try { eval(K1.value); } catch (e){ alert(e.message); } } |
60. 动态执行VBScript脚本
1
2
3
4
5
6
7
8
9
|
function
try { var
"K1" ).value; if (script.trim()== "" ) return ; window.execScript( 'On +script+ '\n , "vbscript" ) } catch (e){ alert(e.message); } } |
61. 金额大写转换函数
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
|
function
try
var
var
new
"" , "万" , "亿" ); //大单位 var
new
"拾" , "佰" , "仟" ); //小单位 var
new
"零" , "壹" , "贰" , "叁" , "肆" , "伍" , "陆" , "柒" , "捌" , "玖" ); //整数部分用 //以下是小写转换成大写显示在合计大写的文本框中 //分离整数与小数 var
var
var
//转换整数部分 var
//计小单位 var
//计大单位 var
var
"" ; var
//整数的长度 for
var
//取得某个位数上的数字 var
if
bn //取得某个位数前一位上的数字 } sum if
str //取得该数字对应的大写数字,并插入到str字符串的前面 if
'0' ) } if
//在数字范围内 if
//加小单位 if
str } k1++; } else
//不加小单位,加大单位 k1 var
if
"万"
"亿" ) //若大单位前没有数字则舍去大单位 str str sum } } if
//小单位到千则大单位进一 k2++; } } //转换小数部分 var
"" ; if
"" ) var
if
strdig "角" ; //加数字 } var
if
strdig "分" ; //加数字 } } str "元"
} catch (e) return
; } return
} //拆分整数与小数 function
var
new
'' , '' ); temp "." ); for
var
value } return
} |
62. resize的操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
( function (){ var
function (){ var
,r ,b ,classname if (w //当窗体的宽度小于1255的时候执行相应的操作 } else { //当窗体的宽度大于1255的时候执行相应的操作 } } if (window.addEventListener){ window.addEventListener( 'resize' , function (){ } else
(window.attachEvent){ window.attachEvent( 'onresize' , function (){ } fn(); })(); |
63. 实现base64解码
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
|
function
var
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" ; var
"" ,tmp_arr if
return
data '' ; do
h1 h2 h3 h4 bits o1 o2 o3 if
tmp_arr[ac++] } else
tmp_arr[ac++] } else
tmp_arr[ac++] } } while
dec '' ); dec return
} |
64. 实现utf8解码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function
var
'' ; while
c1 if
tmp_arr[ac++] i++; } else
c2 tmp_arr[ac++] i } else
c2 c3 tmp_arr[ac++] i } } return
'' ); } |
65. 获取窗体可见范围的宽与高
1
2
3
4
5
6
7
|
function
var
var
var
var
return
} |
66. 断鼠标是否移出事件
1
2
3
4
5
6
7
8
9
10
|
function
if
'mouseout' ) return
; } var
'mouseout'
while
reltg } return
} |
67. 半角转换为全角函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
function
var
'' ; for ( var
code if (code result } else
result } else { result } } return
} |
68. 全角转换为半角函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
function
var
'' ; for ( var
code if (code result } else
result } else { result } } return
} |
最新文章
- 前端性能优化的另一种方式——HTTP2.0
- 【转】【MySql】MySql优化要点
- Android Studio的git功能的使用介绍
- Retrofit 2.1 入门
- 【JS复习笔记】02 对象与函数
- sql server 修改表自增列的值
- 遇到bug我会怎么做
- javaCV图像处理之Frame、Mat和IplImage三者相互转换(使用openCV进行Mat和IplImage转换)
- R-CNN论文翻译——用于精确物体定位和语义分割的丰富特征层次结构
- Python学习 Part7:类
- 2018 CCPC 网络赛 Buy and Resell
- 第四课 VMP壳内爆破
- js window.open隐藏参数提交
- 一篇文章,读懂Netty的高性能架构之道
- java基础之运算符与语句
- 腾讯下载的视频qlv格式转化为MP4格式
- 卡特兰数Catalan——定义、公式、模型总结
- 一、Open CV3.0.0 与 VS2012配置
- CCF 201409-4 最优配餐
- Anaconda( different versions) configuration in ubuntu 14