栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

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

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

工作中常用的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、在文本框中光标位置插入文本值


(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);

调用方式:这里使用了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代码:

 

$("#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());

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/91626.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号