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

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

1、获取URL请求参数

rush:js;"> //根据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、在文本框中光标位置插入文本值

rush:js;"> /* 在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富文本控件

rush:js;"> $("#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代码

rush:xhtml;">

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

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

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

= 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字符串)

rush:js;"> //方式一 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());

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程之家!

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...