常用js归纳

一、获取地址栏参数

1 /*根据name获取URL参数*/
2     function getQueryString(name) {
3         var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
4         var r = window.location.search.substr(1).match(reg);
5         if (r != null) return unescape(r[2]); return null;
6     }
View Code

二、提取文本中中文

1 <script type="text/javascript">   
2 var str="怎样从一个Html页面中提取所有汉字呢?不能有其它Html代码。";  
3 str=str.replace(/[^u4e00-u9fa5]/gi,"");  
4 </script> 
View Code

 三、数组去重

 1 //数组去重
 2     Array.prototype.uniqueNo = function()
 3     {
 4         this.sort();
 5         var re=[this[0]];
 6         for(var i = 1; i < this.length; i++)
 7         {
 8             if( this[i] !== re[re.length-1])
 9             {
10                 re.push(this[i]);
11             }
12         }
13         return re;
14     }
View Code

 四、整数正则验证

 onkeyup="this.value=this.value.replace(/D/g,'')"
View Code

五、小数正则验证 

 var pattern = /^d+(.d+)?$/;
View Code

 六、绑定回车事件

1 document.onkeydown = function (event) {
2 e = event ? event : (window.event ? window.event : null);
3 if (e.keyCode == 13) {
4 $('#submit').click();
5 }
6 }
View Code

七、获取当前时间(yyyy-dd-mm hh:mm:ss)

 1 function getNowFormatDate() {
 2         var date = new Date();
 3         var seperator1 = "-";
 4         var seperator2 = ":";
 5         var month = date.getMonth() + 1;
 6         var strDate = date.getDate();
 7         if (month >= 1 && month <= 9) {
 8             month = "0" + month;
 9         }
10         if (strDate >= 0 && strDate <= 9) {
11             strDate = "0" + strDate;
12         }
13         var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
14                 + " " + date.getHours() + seperator2 + date.getMinutes()
15                 + seperator2 + date.getSeconds();
16         return currentdate;
17     }
View Code

八、生成32位GUID

 1 function GUID() {
 2         this.date = new Date();   /* 判断是否初始化过,如果初始化过以下代码,则以下代码将不再执行,实际中只执行一次 */
 3         if (typeof this.newGUID != 'function') {   /* 生成GUID码 */
 4             GUID.prototype.newGUID = function () {
 5                 this.date = new Date(); var guidStr = '';
 6                 sexadecimalDate = this.hexadecimal(this.getGUIDDate(), 16);
 7                 sexadecimalTime = this.hexadecimal(this.getGUIDTime(), 16);
 8                 for (var i = 0; i < 9; i++) {
 9                     guidStr += Math.floor(Math.random() * 16).toString(16);
10                 }
11                 guidStr += sexadecimalDate;
12                 guidStr += sexadecimalTime;
13                 while (guidStr.length < 32) {
14                     guidStr += Math.floor(Math.random() * 16).toString(16);
15                 }
16                 return this.formatGUID(guidStr);
17             }
18             /* * 功能:获取当前日期的GUID格式,即8位数的日期:19700101 * 返回值:返回GUID日期格式的字条串 */
19             GUID.prototype.getGUIDDate = function () {
20                 return this.date.getFullYear() + this.addZero(this.date.getMonth() + 1) + this.addZero(this.date.getDay());
21             }
22             /* * 功能:获取当前时间的GUID格式,即8位数的时间,包括毫秒,毫秒为2位数:12300933 * 返回值:返回GUID日期格式的字条串 */
23             GUID.prototype.getGUIDTime = function () {
24                 return this.addZero(this.date.getHours()) + this.addZero(this.date.getMinutes()) + this.addZero(this.date.getSeconds()) + this.addZero(parseInt(this.date.getMilliseconds() / 10));
25             }
26             /* * 功能: 为一位数的正整数前面添加0,如果是可以转成非NaN数字的字符串也可以实现 * 参数: 参数表示准备再前面添加0的数字或可以转换成数字的字符串 * 返回值: 如果符合条件,返回添加0后的字条串类型,否则返回自身的字符串 */
27             GUID.prototype.addZero = function (num) {
28                 if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) {
29                     return '0' + Math.floor(num);
30                 } else {
31                     return num.toString();
32                 }
33             }
34             /*  * 功能:将y进制的数值,转换为x进制的数值 * 参数:第1个参数表示欲转换的数值;第2个参数表示欲转换的进制;第3个参数可选,表示当前的进制数,如不写则为10 * 返回值:返回转换后的字符串 */GUID.prototype.hexadecimal = function (num, x, y) {
35                 if (y != undefined) { return parseInt(num.toString(), y).toString(x); }
36                 else { return parseInt(num.toString()).toString(x); }
37             }
38             /* * 功能:格式化32位的字符串为GUID模式的字符串 * 参数:第1个参数表示32位的字符串 * 返回值:标准GUID格式的字符串 */
39             GUID.prototype.formatGUID = function (guidStr) {
40                 var str1 = guidStr.slice(0, 8), str2 = guidStr.slice(8, 12), str3 = guidStr.slice(12, 16), str4 = guidStr.slice(16, 20), str5 = guidStr.slice(20);
41                 return str1 + str2 + str3 + str4 + str5;
42             }
43         }
44     }
View Code

九、UTF8编码

1 //UTF字符转换
2 var UTFTranslate = {
3 Change:function(pValue){
4 return pValue.replace(/[^u0000-u00FF]/g,function($0){return escape($0).replace(/(%u)(w{4})/gi,"&#x$2;")});
5 },
6 ReChange:function(pValue){
7 return unescape(pValue.replace(/&#x/g,'%u').replace(/\u/g,'%u').replace(/;/g,''));
8 }
9 };
View Code

十、json日期格式化

//yyyy-MM-dd HH:mm:SS
    function getDateTime(date) {
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hh = date.getHours();
        var mm = date.getMinutes();
        var ss = date.getSeconds();
        if (day < 10)
            day = '0' + day;
        if (month < 10)
            month = '0' + month;
        if (hh < 10)
            hh = '0' + hh;
        if (mm < 10)
            mm = '0' + mm;
        if (ss < 10)
            ss = '0' + ss;
        return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
    }
    function ConvertJSONDateToJSDate(jsondate) {
        var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
        return date;
    }
View Code  
原文地址:https://www.cnblogs.com/surfing/p/4972198.html