工作中常用的js、jquery自定义扩展函数代码片段汇总
仅记录一些我工作中常用的自定义js函数。
1、获取URL请求参数
//根据URL获取Id
functionGetQueryString(name){
varreg=newRegExp("(^|&)"+name+"=([^&]*)(&|$)");
varr=window.location.search.substr(1).match(reg);
if(r!=null)returnunescape(r[2]);return"";
}
调用方式:varid=GetQueryString("id");
2、在文本框中光标位置插入文本值
/*在textarea处插入文本--Start*/
(function($){
$.fn.extend({
"insertContent":function(myValue,t){
var$t=$(this)[0];
if(document.selection){//ie
this.focus();
varsel=document.selection.createRange();
sel.text=myValue;
this.focus();
sel.moveStart('character',-l);
varwee=sel.text.length;
if(arguments.length==2){
varl=$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();
}
}elseif($t.selectionStart
||$t.selectionStart=='0'){
varstartPos=$t.selectionStart;
varendPos=$t.selectionEnd;
varscrollTop=$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){
varitem=$('#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代码:
<selectclass="easyui-combobox"id="sltLabel"name="sltLabel"style="width:150px"onselect="change()"data-options="panelWidth:150,panelHeight:'auto',valueField:'Value',textField:'Text'"> <optionvalue="">选择要插入的标签</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位的数字)
//例子:
//(newDate()).Format("yyyy-MM-ddhh:mm:ss.S")==>2006-07-0208:09:04.423
//(newDate()).Format("yyyy-M-dh:m:s.S")==>2006-7-28:9:4.18
Date.prototype.Format=function(fmt){//author:zouqj
varo={
"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(varkino)
if(newRegExp("("+k+")").test(fmt))fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)?(o[k]):(("00"+o[k]).substr((""+o[k]).length)));
returnfmt;
}
调用方式:newDate(json.ModifiedOn).Format("yyyy-MM-ddhh:mm:ss")
4、获取当前时间,格式:yyyy-MM-ddhh:mm:ss
//获取当前时间,格式:yyyy-MM-ddhh:mm:ss
functiongetNowFormatDate(){
vardate=newDate();
varseperator1="-";
varseperator2=":";
varmonth=date.getMonth()+1;
varstrDate=date.getDate();
if(month>=1&&month<=9){
month="0"+month;
}
if(strDate>=0&&strDate<=9){
strDate="0"+strDate;
}
varcurrentdate=date.getFullYear()+seperator1+month+seperator1+strDate
+""+date.getHours()+seperator2+date.getMinutes()
+seperator2+date.getSeconds();
returncurrentdate;
}
5、 生成一个由随机数组成的伪Guid(32位Guid字符串)
//方式一
functionnewPseudoGuid(){
varguid="";
for(vari=1;i<=32;i++){
varn=Math.floor(Math.random()*16.0).toString(16);
guid+=n;
if((i==8)||(i==12)||(i==16)||(i==20))
guid+="-";
}
returnguid;
}
//方式二
functionS4(){
return(((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
//生成guid
functionguid(){
return(S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!