工作中常用的js、jquery自定义扩展函数代码片段

仅记录一些我工作中常用的自定义js函数。

1、获取URL请求参数

//根据URL获取Id
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return "";
}

调用方式:var id = GetQueryString("id");

2、在文本框中光标位置插入文本值

/* 在textarea处插入文本--Start */
(function ($) {
    $.fn.extend({
        "insertContent": function (myValue, t) {
            var $t = $(this)[0];
            if (document.selection) { // ie  
                this.focus();
                var sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
                sel.moveStart('character', -l);
                var wee = sel.text.length;
                if (arguments.length == 2) {
                    var l = $t.value.length;
                    sel.moveEnd("character", wee + t);
                    t <= 0 ? sel.moveStart("character", wee - 2 * t - myValue.length) : sel.moveStart("character", wee - t - myValue.length);
                    sel.select();
                }
            } else if ($t.selectionStart
             || $t.selectionStart == '0') {
                var startPos = $t.selectionStart;
                var endPos = $t.selectionEnd;
                var scrollTop = $t.scrollTop;
                $t.value = $t.value.substring(0, startPos)
                 + myValue
                 + $t.value.substring(endPos, $t.value.length);
                this.focus();
                $t.selectionStart = startPos + myValue.length;
                $t.selectionEnd = startPos + myValue.length;
                $t.scrollTop = scrollTop;
                if (arguments.length == 2) {
                    $t.setSelectionRange(startPos - t,
                     $t.selectionEnd + t);
                    this.focus();
                }
            } else {
                this.value += myValue;
                this.focus();
            }
        }
    })
})(jQuery);
/* 在textarea处插入文本--Ending */

调用方式:这里使用了easyui中的combobox控件和ueditor富文本控件

            $("#sltLabel").combobox({
                onSelect: function (item) {
                    var item = $('#sltLabel').combobox('getValue');
                    if (item != undefined && item != null && item != "") {
                        if ($("#sltChannel").val() == 0) {
                            UE.getEditor('editor').focus();
                            UE.getEditor('editor').execCommand('inserthtml', '{' + item + '}');
                        } else {
                            $("#txtContent").insertContent('{' + item + '}');
                        }
                    }
                }
            });

easyui-combobox代码:

  <select class="easyui-combobox" id="sltLabel" name="sltLabel" style="width: 150px" onselect="change()" data-options="panelWidth: 150,panelHeight: 'auto',valueField: 'Value',textField: 'Text'">
  <option value="">选择要插入的标签</option></select>

 $("#sltLabel").combobox("loadData", data);

3、将 Date 转化为指定格式的String

// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子: 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: zouqj 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //
        "s+": this.getSeconds(), //
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

调用方式:new Date(json.ModifiedOn).Format("yyyy-MM-dd hh:mm:ss")

4、获取当前时间,格式:yyyy-MM-dd hh:mm:ss

//获取当前时间,格式:yyyy-MM-dd hh:mm:ss
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}

5、  生成一个由随机数组成的伪Guid(32位Guid字符串)

//方式一
function newPseudoGuid () {
            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;
        }
//方式二
function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
//生成guid
function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

 

你可能感兴趣的:(工作中常用的js、jquery自定义扩展函数代码片段)