1 /// <reference path="jquery-1.8.0.min.js" />
2 /*
3 * DIV或元素居中
4 * @return
5 */
6 jQuery.fn.mCenterDiv = function () {
7 this.css("position", "absolute");
8 this.css("border", "1px solid #ccc");
9 this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
10 this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
11 this.show(100);
12 return this;
13 };
14
15 /*
16 * 替换字符串中所有符合的字符
17 * @param ASource 源字符串
18 * @param AFindText 待替换字符
19 * @param ARepText 替换后字符
20 * @return
21 */
22 jQuery.mReplaceAll = function (ASource,AFindText, ARepText) {
23 var raRegExp = new RegExp(AFindText, "g");
24 return ASource.replace(raRegExp, ARepText);
25 };
26
27 /*
28 * 判断object是否空,未定义或null
29 * @param object
30 * @return
31 */
32 jQuery.mIsNull = function (obj) {
33 if (obj == "" || typeof(obj) == "undefined" || obj == null) {
34 return true;
35 }
36 else {
37 return false;
38 }
39 };
40
41 /*
42 * 获取URL参数
43 * @param name 参数
44 * @return
45 */
46 jQuery.mGetUrlParam = function (name) {
47 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
48 var r = window.location.search.substr(1).match(reg);
49 if (r != null) return unescape(r[2]); return null;
50 };
51
52 /*
53 * 乘法函数,用来得到精确的乘法结果
54 * @param arg1 参数1
55 * @param arg2 参数2
56 * @return
57 */
58 jQuery.mAccMul = function(arg1, arg2) {
59 var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
60 try { m += s1.split(".")[1].length } catch (e) { }
61 try { m += s2.split(".")[1].length } catch (e) { }
62 return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
63 }
64
65 /*
66 * 获取随机数
67 * @param x 下限
68 * @param y 上限
69 * @return
70 */
71 jQuery.mGetRandom = function (x, y) {
72 return parseInt(Math.random() * (y - x + 1) + x);
73
74 };
75
76 /*
77 * 将数值四舍五入(保留2位小数)后格式化成金额形式
78 * @param num 数值(Number或者String)
79 * @return 金额格式的字符串,如'1,234,567.45'
80 */
81 jQuery.mFormatCurrency = function(num) {
82 num = num.toString().replace(/\$|\,/g, '');
83 if (isNaN(num))
84 num = "0";
85 sign = (num == (num = Math.abs(num)));
86 num = Math.floor(num * 100 + 0.50000000001);
87 cents = num % 100;
88 num = Math.floor(num / 100).toString();
89 if (cents < 10)
90 cents = "0" + cents;
91 for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
92 num = num.substring(0, num.length - (4 * i + 3)) + ',' +
93 num.substring(num.length - (4 * i + 3));
94 return (((sign) ? '' : '-') + num + '.' + cents);
95 }
96
97 /*
98 * 正则验证
99 * @param s 验证字符串
100 * @param type 验证类型 money,china,mobile等
101 * @return
102 */
103 jQuery.mCheck = function (s, type) {
104 var objbool = false;
105 var objexp = "";
106 switch (type) {
107 case 'money': //金额格式,格式定义为带小数的正数,小数点后最多三位
108 objexp = "^[0-9]+[\.][0-9]{0,3}$";
109 break;
110 case 'numletter_': //英文字母和数字和下划线组成
111 objexp = "^[0-9a-zA-Z\_]+$";
112 break;
113 case 'numletter': //英文字母和数字组成
114 objexp = "^[0-9a-zA-Z]+$";
115 break;
116 case 'numletterchina': //汉字、字母、数字组成
117 objexp = "^[0-9a-zA-Z\u4e00-\u9fa5]+$";
118 break;
119 case 'email': //邮件地址格式
120 objexp = "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$";
121 break;
122 case 'tel': //固话格式
123 objexp = /^((\d2,3)|(\d{3}\-))?(0\d2,3|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/;
124 break;
125 case 'mobile': //手机号码
126 objexp = "^(13[0-9]|15[0-9]|18[0-9])([0-9]{8})$";
127 break;
128 case 'decimal': //浮点数
129 objexp = "^[0-9]+([.][0-9]+)?$";
130 break;
131 case 'url': //网址
132 objexp = "(http://|https://){0,1}[\w\/\.\?\&\=]+";
133 break;
134 case 'date': //日期 YYYY-MM-DD格式
135 objexp = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
136 break;
137 case 'int': //整数
138 objexp = "^[0-9]*[1-9][0-9]*$";
139 break;
140 case 'int+': //正整数包含0
141 objexp = "^\\d+$";
142 break;
143 case 'int-': //负整数包含0
144 objexp = "^((-\\d+)|(0+))$";
145 break;
146 case 'china': //中文
147 objexp = /^[\u0391-\uFFE5]+$/;
148 break;
149 }
150 var re = new RegExp(objexp);
151 if (re.test(s)) {
152 return true;
153 }
154 else {
155 return false;
156 }
157 };
158
159 /*
160 * 获取控件的值
161 * @param controlID 控件ID
162 * @param controltype 类型 如text radio
163 * @return
164 */
165 jQuery.mGetValue = function (controlID, controltype) {
166 var objValue = "";
167 switch (controltype) {
168 case 'text': //文本输入框
169 objValue = $.trim($("#" + controlID + "").attr("value")); //取值去左右空格
170 break;
171 case 'radio': //单选框
172 objValue = $("input[name='" + controlID + "']:checked").attr("value");
173 break;
174 case 'select': //下拉列表
175 objValue = $("#" + controlID + "").attr("value");
176 break;
177 case 'checkbox': //多选框
178 $("input[name='" + controlID + "']:checked").each(function () {
179 objValue += $(this).val() + ",";
180 });
181 break;
182 default:
183 break;
184 }
185 return objValue;
186 };
187
188 /*
189 * 设置控件的值
190 * @param controlID 控件ID
191 * @param controltype 类型 如text radio
192 * @param controlvalue 绑定值
193 * @return
194 */
195 jQuery.mSetValue = function (controlID, controltype, controlvalue) {
196 switch (controltype) {
197 case 'text': //文本输入框
198 //$("#txtUserID").attr("value", '这是绑定内容'); //填充内容
199 //$("input[name='radio1'][value='上海']").attr("checked", true); //单选组radio:设置value='上海'的项目为当前选中项
200 //$("#select1").attr("value", '葡萄牙'); //下拉框select:设置value='中国'的项目为当前选中项
201 //$("input[name='checkbox1'][value='黑色'],[value='蓝色']").attr("checked", true); //多选框:设置多个值为当前选中项
202 $("#" + controlID + "").attr("value", controlvalue); //填充内容
203 break;
204 case 'radio': //单选框
205 $("input[name='" + controlID + "'][value='" + controlvalue + "']").attr("checked", true);
206 break;
207 case 'select': //下拉列表
208 $("#" + controlID + "").attr("value", controlvalue);
209 break;
210 case 'checkbox': //多选框
211 $("input[name='" + controlID + "'][value='" + controlvalue + "'],[value='" + controlvalue + "']").attr("checked", true); //多选框:设置多个值为当前选中项
212 break;
213 default:
214 break;
215 }
216 };
217
218 /*
219 * 兼容IE火狐等浏览器的自动跳转
220 * @param url 跳转网址
221 * @return
222 */
223 jQuery.mAutoNav = function (url) {
224 if ($.browser.msie) {
225 var referLink = document.createElement('a');
226 referLink.href = url;
227 document.body.appendChild(referLink);
228 referLink.click();
229 } else {
230 location.href = url;
231 }
232 };
233
234 /*
235 * Table表格奇偶行设置颜色及移动鼠标行变色
236 * @param table 表格ID
237 * @return
238 */
239 jQuery.mTableHover = function (table) {
240 $("#" + table).each(function () {
241 var o = $(this);
242 //设置偶数行和奇数行颜色
243 o.find("tr:even").css("background-color", "#EFF3FB");
244 o.find("tr:odd").css("background-color", "#FFFFFF");
245 //鼠标移动隔行变色hover用法关键
246 o.find("tr:not(:first)").hover(function () {
247 $(this).attr("bColor", $(this).css("background-color")).css("background-color", "#E0E0E0");
248 }, function () {
249 $(this).css("background-color", $(this).attr("bColor"));
250 });
251 });
252 };
253
254 /*
255 * gridview 隔行换色 鼠标滑过变色 多选
256 * c#获取选择值 Request.Form.Get("chkItem")
257 * @param objgridview ID
258 * @return
259 */
260 jQuery.mGridview = function (objgridview) {
261 var headcolor = { background: '#E0ECFF', color: '#333' };
262 var normalcolor = { background: '#f7f6f3' };
263 var altercolor = { background: '#EDF1F8' };
264 var hovercolor = { background: '#89A5D1' };
265 var selectcolor = { background: '#ACBFDF' };
266 var nullcolor = {};
267 //get obj id
268 var gridviewId = "#" + objgridview;
269 //even
270 $(gridviewId + ">tbody tr:even").css(normalcolor);
271 //first
272 $(gridviewId + ">tbody tr:first").css(nullcolor).css(headcolor);
273 //odd
274 $(gridviewId + ">tbody tr:odd").css(altercolor);
275 //hover
276 $(gridviewId + ">tbody tr").click(function () {
277 var cb = $(this).find("input:checkbox");
278 var chf = typeof (cb.attr("checked")) == "undefined" ? true : false;
279 cb.attr("checked", chf);
280 var expr1 = gridviewId + ' >tbody >tr >td >input:checkbox:checked';
281 var expr2 = gridviewId + ' >tbody >tr >td >input:checkbox';
282 var selectAll = $(expr1).length == $(expr2).length;
283 $('#chkAll').attr('checked', selectAll);
284 }).hover(function () {
285 $(this).css(hovercolor);
286 }, function () {
287 $(gridviewId + ">tbody tr:even").css(normalcolor);
288 $(gridviewId + ">tbody tr:first").css(nullcolor).css(headcolor);
289 $(gridviewId + ">tbody tr:odd").css(altercolor);
290 });
291
292 //all check
293 $("#chkAll").click(function () {
294 $(gridviewId + '>tbody >tr >td >input:checkbox:visible').attr('checked', this.checked);
295 });
296 //check status
297 $(gridviewId + ' >tbody >tr >td >input:checkbox').click(function () {
298 var cb = $(this);
299 var chf = typeof (cb.attr("checked")) == "undefined" ? true : false;
300 cb.attr("checked", chf);
301 var expr1 = gridviewId + ' >tbody >tr >td >input:checkbox:checked';
302 var expr2 = gridviewId + ' >tbody >tr >td >input:checkbox';
303 var selectAll = $(expr1).length == $(expr2).length;
304 $('#chkAll').attr('checked', selectAll);
305 });
306 };
307
308 /*
309 * 屏幕居中显示处理进度
310 * @param info 显示文字
311 * @param type 方式 0遮罩 1不遮罩
312 * @param typepic 图片 0:load 1:ok 2:error
313 * @return
314 */
315 jQuery.mMaskLoad = function (info, type, typepic) {
316 var pic = "";
317 switch (typepic) {
318 case 0: // loading
319 pic = "./Images/loading.gif";
320 break;
321 case 1: // ok
322 pic = "./Images/right.png";
323 break;
324 case 2: // error
325 pic = "./Images/error.png";
326 break;
327 default: //其他任何值时
328 pic = "./Images/loading.gif";
329 break;
330 }
331 if (type == 0) {
332 $("<div class=\"datagrid-mask\"></div>").css(
333 {
334 display: "block",
335 width: "100%",
336 position: "absolute",
337 left: "0",
338 top: "0",
339 opacity: "0.3",
340 height: "100%",
341 filter: "alpha(opacity=30)",
342 background: "#ccc"
343 }).appendTo("body");
344 };
345 $("<div class=\"datagrid-mask-msg\"></div>").css(
346 {
347 position: "absolute",
348 top: "50%",
349 padding: "12px 5px 10px 30px",
350 width: "auto",
351 height: "16px",
352 border: "1px solid #D1D1D1",
353 background: "#ffffff url('" + pic + "') no-repeat scroll 5px center",
354 display: "block",
355 left: ($(document.body).outerWidth(true) - 190) / 2,
356 top: ($(window).height() - 45) / 2
357 }).html(info).appendTo("body");
358 };
359
360 /*
361 * 屏幕居中隐藏处理进度
362 * @return
363 */
364 jQuery.mMaskLoadClose = function () {
365 $(".datagrid-mask").remove();
366 $(".datagrid-mask-msg").remove();
367 };
368
369 /*
370 * 控件后创建SPAN作为TIP提示
371 * @param o 用this
372 * @param tip 提示文字
373 * @param typepic 图片 0:load 1:ok 2:error
374 * @return
375 */
376 jQuery.mTip = function (o, tip, typepic) {
377 var pic = "";
378 switch (typepic) {
379 case 0: // loading
380 pic = "./Images/loading.gif";
381 break;
382 case 1: // ok
383 pic = "./Images/right.png";
384 break;
385 case 2: // error
386 pic = "./Images/error.png";
387 break;
388 default: //其他任何值时
389 pic = "./Images/loading.gif";
390 break;
391 }
392 var eTip = document.createElement("span");
393 var objid = $(o).attr("id") + "_tipDiv";
394 var value = $(o).val();
395 //绝对路径
396 var x = $(o).offset().top;
397 var y = $(o).offset().left;
398 var w = $(o).width();
399 var h = $(o).height();
400 eTip.setAttribute("id", objid);
401 try {
402 document.body.appendChild(eTip);
403 } catch (e) { }
404 $("#" + objid).hide();
405 $("#" + objid).css({
406 top: x,
407 left: y + w + 10,
408 height: h,
409 position: "absolute"
410 });
411 $("#" + objid).html("<img src=\"" + pic + "\" style=\"vertical-align:bottom;margin-right:5px;\">" + tip);
412 $("#" + objid).show();
413 };
414
415 /**
416 * ajax post提交
417 * @param url
418 * @param param
419 * @param datat 为html,json,text
420 * @param callback 回调函数 function callBack(data)
421 * @return
422 */
423 jQuery.mJqAjax = function (url, param, datat, callback) {
424 $.ajax({
425 type: "post",
426 url: url,
427 data: param,
428 dataType: datat,
429 success: callback,
430 error: function () { }
431 });
432 };
433
434
435 [javascript] view plain copy print?在CODE上查看代码片派生到我的代码片
436 /// <reference path="jquery-1.8.3.min.js" />
437 /*********************************************************************/
438 /***************************Jquery 扩展****************************/
439 /*********************************************************************/
440 jQuery.mIsNull = function (obj) {
441 if (obj == "" || typeof (obj) == "undefined" || obj == null) {
442 return true;
443 }
444 else {
445 return false;
446 }
447 };
448
449 jQuery.mCheckNull = function (id, tipid, nullmess, ctype) {
450 var str = $.mGetValue(id, ctype);
451 var tid = ($.mIsNull(tipid)) ? id : tipid;
452 var obj = ($.mIsNull(tipid)) ? $.mTip : $.mTipCustom;
453 if ($.mIsNull(str)) {
454 obj("#" + tid, nullmess, 2);
455 }
456 else {
457 obj("#" + tid, "", 1);
458 }
459 };
460
461 jQuery.mCheckNullAndReg = function (id, tipid, nullmess, regmess, ctype, rtype) {
462 var str = $.mGetValue(id, ctype);
463 var tid = ($.mIsNull(tipid)) ? id : tipid;
464 var obj = ($.mIsNull(tipid)) ? $.mTip : $.mTipCustom;
465 if ($.mIsNull(str)) {
466 obj("#" + tid, nullmess, 2);
467 }
468 else {
469 if ($.mCheck(str, rtype)) {
470 obj("#" + tid, "", 1);
471 } else {
472 obj("#" + tid, regmess, 2);
473 }
474 }
475 };
476
477 jQuery.mCheck = function (s, type) {
478 var objbool = false;
479 var objexp = "";
480 switch (type) {
481 case 'money': //金额格式,格式定义为带小数的正数,小数点后最多三位
482 objexp = "^[0-9]+[\.][0-9]{0,3}$";
483 break;
484 case 'numletter_': //英文字母和数字和下划线组成
485 objexp = "^[0-9a-zA-Z\_]+$";
486 break;
487 case 'numletter': //英文字母和数字组成
488 objexp = "^[0-9a-zA-Z]+$";
489 break;
490 case 'numletterchina': //汉字、字母、数字组成
491 objexp = "^[0-9a-zA-Z\u4e00-\u9fa5]+$";
492 break;
493 case 'email': //邮件地址格式
494 objexp = "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$";
495 break;
496 case 'tel': //固话格式
497 objexp = /^((\d2,3)|(\d{3}\-))?(0\d2,3|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/;
498 break;
499 case 'mobile': //手机号码
500 objexp = "^(13[0-9]|15[0-9]|18[0-9])([0-9]{8})$";
501 break;
502 case 'decimal': //浮点数
503 objexp = "^[0-9]+([.][0-9]+)?$";
504 break;
505 case 'url': //网址
506 objexp = "(http://|https://){0,1}[\w\/\.\?\&\=]+";
507 break;
508 case 'date': //日期 YYYY-MM-DD格式
509 objexp = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
510 break;
511 case 'int': //整数
512 objexp = "^[0-9]*[1-9][0-9]*$";
513 break;
514 case 'int+': //正整数包含0
515 objexp = "^\\d+$";
516 break;
517 case 'int-': //负整数包含0
518 objexp = "^((-\\d+)|(0+))$";
519 break;
520 case 'china': //中文
521 objexp = /^[\u0391-\uFFE5]+$/;
522 break;
523 }
524 var re = new RegExp(objexp);
525 if (re.test(s)) {
526 return true;
527 }
528 else {
529 return false;
530 }
531 };
532
533 jQuery.mTip = function (o, tip, typepic) {
534 var pic = "";
535 switch (typepic) {
536 case 0: // loading
537 pic = "/images/publicNew/loading.gif";
538 break;
539 case 1: // ok
540 pic = "/images/publicNew/right.png";
541 break;
542 case 2: // error
543 pic = "/images/publicNew/error.png";
544 break;
545 default: //其他任何值时
546 pic = "/images/publicNew/onLoad.gif";
547 break;
548 }
549 var eTip = document.createElement("span");
550 var objid = $(o).attr("id") + "_tipDiv";
551 var value = $(o).val();
552 //绝对路径
553 var x = $(o).offset().top;
554 var y = $(o).offset().left;
555 var w = $(o).width();
556 var h = $(o).height();
557 eTip.setAttribute("id", objid);
558 try {
559 document.body.appendChild(eTip);
560 } catch (e) { }
561 $("#" + objid).hide();
562 $("#" + objid).css({
563 top: x,
564 left: y + w + 10,
565 height: h,
566 position: "absolute"
567 });
568 $("#" + objid).html("<img src=\"" + pic + "\" style=\"vertical-align:bottom;margin-right:5px;\">" + tip);
569 $("#" + objid).show();
570 };
571
572 jQuery.mTipCustom = function (o, tip, typepic) {
573 var pic = "";
574 switch (typepic) {
575 case 0: // loading
576 pic = "/images/publicNew/loading.gif";
577 break;
578 case 1: // ok
579 pic = "/images/publicNew/right.png";
580 break;
581 case 2: // error
582 pic = "/images/publicNew/error.png";
583 break;
584 default: //其他任何值时
585 pic = "/images/publicNew/onLoad.gif";
586 break;
587 }
588 $("#" + o).html("<img src=\"" + pic + "\" style=\"vertical-align:bottom;margin-right:5px;\">" + tip);
589 $("#" + o).show();
590 };
591
592 jQuery.mGetValue = function (controlID, controltype) {
593 var objValue = "";
594 switch (controltype) {
595 case 'text': //文本输入框
596 objValue = $.trim($("#" + controlID + "").attr("value")); //取值去左右空格
597 break;
598 case 'radio': //单选框
599 objValue = $("input[name='" + controlID + "']:checked").attr("value");
600 break;
601 case 'select': //下拉列表
602 objValue = $("#" + controlID + "").attr("value");
603 break;
604 case 'checkbox': //多选框
605 $("input[name='" + controlID + "']:checked").each(function () {
606 objValue += $(this).val() + ",";
607 });
608 break;
609 default:
610 break;
611 }
612 return objValue;
613 };
614
615 /**
616 * ajax post提交
617 * @param url
618 * @param param
619 * @param datat 为html,json,text
620 * @param callback 回调函数 function callBack(data)
621 * @return
622 */
623 jQuery.mJqAjax = function (url, param, datat, callback) {
624 $.ajax({
625 type: "post",
626 url: url,
627 data: param,
628 dataType: datat,
629 success: callback,
630 error: function () { }
631 });
632 };