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

时间:2022-04-06 08:50:55

仅记录一些我工作中常用的自定义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());
}

其它函数

/**
* 全局函数
* @param {Object} id
*/
var byId = function(id) {
return document.getElementById(id);
}; function addClass(obj, name) { //添加样式函数
obj.className = obj.className + " " + name;
} function siblings(obj) { //获取到除当前按钮以外其他按钮
var sibArr = obj.parentNode.children;
var sibNewArr = [];
for(var i = 0; i < sibArr.length; i++) {
if(sibArr[i] != obj) {
sibNewArr.push(sibArr[i]);
}
}
return sibNewArr;
} function removeClass(obj, name) { //删除样式函数
var classStr = obj.className;
var classArr = classStr.split(" ");
var classNewArr = [];
for(var i = 0; i < classArr.length; i++) {
if(classArr[i] != name) {
classNewArr.push(classArr[i]);
}
}
obj.className = classNewArr.join(" ");
} function haveClass(obj, name) { //是否有样式
return obj.className.indexOf(name) > 0 ? true : false;
}

近7天,近12个月

/**
* 全局函数
* @param {Object} $
* @param {Object} owner
*/
(function($, owner) {
/**
* 获取当前日期的前七天
*/
owner.getSevenDay = function() {
//设置日期,当前日期的前六天
var myDate = new Date(); //获取今天日期
myDate.setDate(myDate.getDate() - 6);
var dateArray = [];
var dateTemp;
var flag = 1;
var td = {
value: '今日',
textStyle: {
backgroundColor: {
image: '../img/login-logo.png'
}
}
};
for(var i = 0; i < 7; i++) {
dateTemp = i == 6 ? td : myDate.getDate(); //(myDate.getMonth() + 1) + "-" +
dateArray.push(dateTemp);
myDate.setDate(myDate.getDate() + flag);
}
return dateArray;
}
/**
* 获取近12个月
*/
owner.getTwelveMonth = function() {
var d = new Date();
var result = [];
for(var i = 0; i < 12; i++) {
d.setMonth(d.getMonth() + 1);
var m = d.getMonth() + 1;
m = m < 10 ? "0" + m : m;
//在这里可以自定义输出的日期格式
result.push(m + '月');
}
return result;
}
}(mui, window.common = {}));