前端最實用、全面的工具類方法

來源:互聯網
上載者:User

標籤:base   status   為什麼   之間   小程式   ble   while   disabled   字串轉換   

1、為什麼要做這件事
我個人最近總是覺得,寫前台代碼都是大量的抄襲,而且每次抄襲的地方都是來自不同的項目,敲代碼的時間往往小於找代碼的時間;
小程式更加加重了前端js在編程中的分量,學好,做好總結是很有必要的;
精良消除代碼的冗餘;
分享是一件,眾樂樂的事;

2、怎麼做這件事
從大配量序中挑選出最常用的js方法匯總;
網上找出常用的方法,收集;
分成大類進行匯總;

3、主要大類

查詢類(ajax請求,表單請求)

校正類(Regex,日期,查重)

資料轉換處理類(日期類,字串類)

加密解密類(base64)


4、現在就開始總結

 

一、查詢類(ajax請求,表單請求)

ajax請求:

requestJsonRs : function(url, param, async, callback) {        if (!param) {            param = {}        }        var jsonObj = null;        $.ajax({            type : "post",            dataType : "html",            url : url,            data : param,            async : (async ? async : false),            success : function(data) {                try {                    jsonObj = eval("(" + data + ")")                } catch (e) {                    jsonObj = null                }                if (callback) {                    callback(jsonObj)                }            },            error : function(XMLHttpRequest, textStatus, errorThrown) {                jsonObj = {                    rtMsg : "Ajax Request Error"                }            },            timeout : 30000        });        return jsonObj    }

二、校正類(Regex,日期,查重)

//檢查手機號function checkMobile(str) { var Str=document.getElementById(str).value; RegularExp=/^[0-9]{11}$/ if (RegularExp.test(Str)) {  return true; } else {  alert("手機號格式不正確!應該為11位長度的數字!");  return false; }}

 

三、資料轉換處理類(日期類,字串類)

字串轉換為json格式:

strToJson : function(str) {        var json = eval("(" + str + ")");        return json    }

表單轉換為Json格式:

formToJson : function(c, b) {        var a = {};        if (!b) {            $(c).find("input[name][disabled!=disabled]").each(function(d, e) {                a[$(e).attr("name")] = $(e).val()            });            $(c).find("input[name][type=hidden][disabled!=disabled]").each(                    function(d, e) {                        a[$(e).attr("name")] = $(e).val()                    });            $(c).find("textarea[name][disabled!=disabled]").each(                    function(d, e) {                        a[$(e).attr("name")] = $(e).val()                    });            $(c).find("select[name][disabled!=disabled]").each(function(d, e) {                a[$(e).attr("name")] = $(e).val()            });            $(c).find("input[name][type=checkbox][disabled!=disabled]").each(                    function(d, e) {                        if ($(e).attr("checked")) {                            a[$(e).attr("name")] = 1                        } else {                            a[$(e).attr("name")] = 0                        }                    });            $(c).find("input[name][type=radio][disabled!=disabled]:checked")                    .each(function(d, e) {                        a[$(e).attr("name")] = $(e).val()                    });            $(c).find("input[name][type=password][disabled!=disabled]").each(                    function(d, e) {                        a[$(e).attr("name")] = $(e).val()                    })        } else {            $(c).find("input[name]").each(function(d, e) {                a[$(e).attr("name")] = $(e).val()            });            $(c).find("input[name][type=hidden]").each(function(d, e) {                a[$(e).attr("name")] = $(e).val()            });            $(c).find("textarea[name]").each(function(d, e) {                a[$(e).attr("name")] = $(e).val()            });            $(c).find("select[name]").each(function(d, e) {                a[$(e).attr("name")] = $(e).val()            });            $(c).find("input[name][type=checkbox]").each(function(d, e) {                if ($(e).attr("checked")) {                    a[$(e).attr("name")] = 1                } else {                    a[$(e).attr("name")] = 0                }            });            $(c).find("input[name][type=radio]:checked").each(function(d, e) {                a[$(e).attr("name")] = $(e).val()            });            $(c).find("input[name][type=password]").each(function(d, e) {                a[$(e).attr("name")] = $(e).val()            })        }        return a    }

四、加密解密類(base64)

Base64位加密

明確幾個概念:
//charCodeAt() 方法可返回指定位置的字元的 Unicode 編碼。這個傳回值是 0 - 65535 之間的整數

//fromCharCode() 可接受一個指定的 Unicode 值,然後返回一個字串。

encode64 : function(d) {        d = strUnicode2Ansi(d);        var b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";        var a = "";        var l, j, g = "";        var k, h, f, e = "";        var c = 0;        do {            l = d.charCodeAt(c++);            j = d.charCodeAt(c++);            g = d.charCodeAt(c++);            k = l >> 2;            h = ((l & 3) << 4) | (j >> 4);            f = ((j & 15) << 2) | (g >> 6);            e = g & 63;            if (isNaN(j)) {                f = e = 64            } else {                if (isNaN(g)) {                    e = 64                }            }            a = a + b.charAt(k) + b.charAt(h) + b.charAt(f) + b.charAt(e);            l = j = g = "";            k = h = f = e = ""        } while (c < d.length);        return a    }    function strUnicode2Ansi(g) {        var c = g.length;        var a = "";        for (var d = 0; d < c; d++) {            var f = g.charCodeAt(d);            if (f < 0) {                f += 65536            }            if (f > 127) {                f = UnicodeToAnsi(f)            }            if (f > 255) {                var b = f & 65280;                b = b >> 8;                var e = f & 255;                a += String.fromCharCode(b) + String.fromCharCode(e)            } else {                a += String.fromCharCode(f)            }        }        return a    }

//強制把大寫轉換成小寫

function toLowCase(){ if(event.keyCode>=65 && event.keyCode<=90)  event.keyCode=event.keyCode+32;}

//將小數四捨五入
//decimalNum 為:保留小數點後幾位

function roundFloat(str, decimalNum) {    var sourceValue=document.getElementById(str).value; if (isNaN(sourceValue)) {     alert("sourceValue非法字元 ");     document.getElementById(str).value="";  return sourceValue; } var multiplyValue=Math.pow(10,parseInt(decimalNum)); document.getElementById(str).value=(Math.round(multiplyValue*sourceValue))/multiplyValue; return (Math.round(multiplyValue*sourceValue))/multiplyValue;}

 

前端最實用、全面的工具類方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.