zoukankan      html  css  js  c++  java
  • javascript 常用方法


    ///生成GUID方法
    function newGuid() {
        var guid = "";
        for (var i = 1; i <= 32; i++) {
            var n = Math.floor(Math.random() * 16.0).toString(16);
            guid += n;
            if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                guid += "-";
        }
        return guid;
    }
    //生成GUID
    function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    function NewGuid() {
        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    }

     
    function lrtrim(value) {
        return value.replace(/(^s*)|(s*$)/g, '');
    }

    //获取当前页面的Action
    function getCurrentAction() {
        var thisHREF = document.location.href;
        var tmpHPage = thisHREF.split("/");
        var currentPage = null == tmpHPage[tmpHPage.length - 1] ? "" : tmpHPage[tmpHPage.length - 1].split("?")[0].split("#")[0];
        return currentPage;
    }

    //获取URL中的参数 name为参数名称
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    /**
    * 异步获取数据
    * @url                         请求的地址
    * example: 'http://www.baidu.com?keyword=name'
    * @type                      请求方式
    * example:'post' 或 'get'
    * @data                      发送给服务器的数据
    * example:{'name':'tom','age':20}
    * @sucCallbackFunc     执行成功后的回调函数
    * example:function(data, textStatus){}
    * @comCallbackFunc   请求完成后的回调函数
    * example:function(xmlHttpRequest, textStatus){}
    * @errCallbackFunc     请求失败后的回调函数
    * example:function(xmlHttpRequest, textStatus, errorThrown){}
    */
    function getDataAsync(url, type, data, sucCallbackFunc, comCallbackFunc, errCallbackFunc) {
        if (url.indexOf("?") >= 0) {
            url = url + "&random=" + Math.random();
        } else {
            url = url + "?random=" + Math.random();
        }
        $.ajax({
            url: url,
            type: type,
            data: data,
            dataType: "json",
            timeout: 15000,
            success: function (data, textStatus) {
                if (sucCallbackFunc) {
                    sucCallbackFunc(data, textStatus);
                }

            },
            complete: function (xmlHttpRequest, textStatus) {
                if (comCallbackFunc) {
                    comCallbackFunc(xmlHttpRequest, textStatus);
                }
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                if (errCallbackFunc) {
                    errCallbackFunc(xmlHttpRequest, textStatus, errorThrown);
                }
                showTipsMsg("数据请求失败,请尝试重新操作!", 4000, 'e');
            }
        });
    }

    /**
    * 同步获取数据
    * @url                         请求的地址
    * example: 'http://www.baidu.com?keyword=name'
    * @type                      请求方式
    * example:'post' 或 'get'
    * @data                      发送给服务器的数据
    * example:{'name':'tom','age':20}
    * @sucCallbackFunc     执行成功后的回调函数
    * example:function(data, textStatus){}
    * @comCallbackFunc   请求完成后的回调函数
    * example:function(xmlHttpRequest, textStatus){}
    * @errCallbackFunc     请求失败后的回调函数
    * example:function(xmlHttpRequest, textStatus, errorThrown){}
    */
    function getDataSync(url, type, data, sucCallbackFunc, comCallbackFunc, errCallbackFunc) {
        if (url.indexOf("?") >= 0) {
            url = url + "&random=" + Math.random();
        } else {
            url = url + "?random=" + Math.random();
        }
        $.ajax({
            url: url,
            type: type,
            data: data,
            dataType: "json",
            async: false,
            success: function (data, textStatus) {
                if (sucCallbackFunc) {
                    sucCallbackFunc(data, textStatus);
                }
            },
            complete: function (xmlHttpRequest, textStatus) {
                if (comCallbackFunc) {
                    comCallbackFunc(xmlHttpRequest, textStatus);
                }
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                if (errCallbackFunc) {
                    errCallbackFunc(xmlHttpRequest, textStatus, errorThrown);
                }
                $.messager.show({
                    title: '系统提示',
                    msg: '数据请求失败,请尝试重新操作!',
                    showType: 'fade',
                    draggable: true,
                    style: {
                        left: '',
                        right: 0,
                        bottom: ''
                    }
                });
            }
        });
    }

    /**
    * 操作状态提示
    * @msg                       提示信息
    * example: '提交成功!'
    * @time                      显示时间
    * example: 4000
    * @type                      提示类型
    * example: 's':操作成功;'e':操作失败;'w':操作警告
    */
    function showTipsMsg(msg, time, type) {
        if (type == 's') {
            top.showTopMsg(msg, time, 'success');
        } else if (type == 'e') {
            top.showTopMsg(msg, time, 'error');
        } else if (type == 'w') {

            top.showTopMsg(msg, time, 'warning');
        }
    }

    function showTopMsg(msg, time, type) {
        MsgTips(time, msg, 300, type);
    }

    function MsgTips(timeOut, msg, speed, type) {
        $(".tip_container").remove();
        var bid = parseInt(Math.random() * 100000);
        $("body").prepend('<div id="tip_container' + bid + '" class="container tip_container"><div id="tip' + bid + '" class="mtip"><span id="tsc' + bid + '"></span></div></div>');
        var $this = $(this);
        var $tip_container = $("#tip_container" + bid);
        var $tip = $("#tip" + bid);
        var $tipSpan = $("#tsc" + bid);
        clearTimeout(window.timer);
        $tip.attr("class", type).addClass("mtip");
        $tipSpan.html(msg);
        $tip_container.slideDown(speed);
        window.timer = setTimeout(function () {
            $tip_container.slideUp(speed);
            $(".tip_container").remove();
        }, timeOut);
        $tip_container.live("mouseover", function () {
            clearTimeout(window.timer);
        });
        $tip_container.live("mouseout", function () {
            window.timer = setTimeout(function () {
                $tip_container.slideUp(speed);
                $(".tip_container").remove();
            }, timeOut);
        });
        $("#tip_container" + bid).css("left", ($(window).width() - $("#tip_container" + bid).width()) / 2);
    }

    //删除数组中指定索引位置的元素
    Array.prototype.remove = function (index) {
        if (isNaN(index) || index >= this.length || this.length == 0 || index<0) {
            return false;
        }
        for (var i = 0, n = 0; i < this.length; i++) {
            if (this[i] != this[index]) {
                this[n++] = this[i]
            }
        }
        this.length -= 1
    }

    //获取某元素在数组中的位置
    Array.prototype.indexOf = function (value, field) {
        var index = -1;
        if (field) {
            for (var i = 0; i < this.length; i++) {
                if (eval("this[i]." + field) == eval("value." + field)) {
                    index = i;
                    break;
                }
            }
        }
        else {
            for (var i = 0; i < this.length; i++) {
                if (this[i] == value) {
                    index = i;
                    break;
                }
            }
        }
        return index;
    }


    function dicHelper(options) {
       
    }

  • 相关阅读:
    PowerDesigner 15 之Check Model报错信息详解
    js出现:类不支持Automation操作
    IIS错误解决整理
    JavaScript页面跳转的几种方法
    JavaScript刷新上一级页面
    SQL 截断字符串查询(CharIndex()函数)
    JS 判断CHECKBOX复选框多选及选中项验证
    SQL 分组后返回序号(ROW_NUMBER () OVER(PARTITION BY order_no ORDER BY START_Time ASC 的使用方法)
    jquery 获取父窗口的元素 父窗口 子窗口[资料来源于网络]
    SQL Case When Then Else End的使用收集
  • 原文地址:https://www.cnblogs.com/dullbaby/p/3629243.html
Copyright © 2011-2022 走看看