1. 判断字符串是否为空

  1. function isEmptyString(str) {
  2. return str == undefined || str == "" || str == null;
  3. }

2. 判断数据格式

2.1 判断是否邮箱格式

  1. function isEmail(str) {
  2. var re = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
  3. return re.test(str);
  4. }

2.2 判断是否整数格式

  1. function isInt(str) {
  2. var re = new RegExp("^-?[0-9]+$");
  3. return re.test(str);
  4. }

2.3 判断是否浮点数字格式

  1. function isFloat(str) {
  2. var re = new RegExp("^-?[0-9]+\.[0-9]+$");
  3. return re.test(str);
  4. }

3. 获取object中属性的值

  1. function getObjectData(object, field) {
  2. var fieldArray = field.split(".");
  3. var childObj = object;
  4. $.each(fieldArray, function(i, n) {
  5. childObj = childObj[n];
  6. })
  7. return childObj;
  8. }

例如:

var test = {a:"aaa",b:{c:"ccc",d:{e:"eee",f:"fff"}}};

var testResult = getObjectData(test,"a.b.d.f");

获得到的testResult的值为"fff"。

4. 扩展日期类型,增加格式化功能

  1. Date.prototype.Format = function(fmt) {
  2. var o = {
  3. "M+" : this.getMonth() + 1,
  4. "d+" : this.getDate(),
  5. "h+" : this.getHours(),
  6. "m+" : this.getMinutes(),
  7. "s+" : this.getSeconds(),
  8. "q+" : Math.floor((this.getMonth() + 3) / 3),
  9. "S" : this.getMilliseconds()
  10. };
  11. if (/(y+)/.test(fmt))
  12. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  13. for ( var k in o)
  14. if (new RegExp("(" + k + ")").test(fmt))
  15. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  16. return fmt;
  17. }

5. 设定整个页面的keyPress事件响应

  1. function setGloableKeyPress(keyCode, onKeyPress) {
  2. document.onkeydown = function(e) {
  3. var ev = document.all ? window.event : e;
  4. if (ev.keyCode == keyCode) {
  5. onKeyPress();
  6. }
  7. }
  8. }

6. form表单功能扩展

6.1 表单输入元素中回车响应

  1. $.fn.formAutoSearch = function(onSearch) {
  2. var id = $(this).attr("id");
  3. $("#" + id + " :input").not(':button, :submit, :reset').keypress(function(e) {
  4. if (e.keyCode == 13) {
  5. onSearch();
  6. }
  7. })
  8. }

6.2 检测表单输入合法性(需要html元素属性支持)

  1. $.fn.formCheck = function(funMsg) {
  2. var rtn = true;
  3.  
  4. var id = $(this).attr("id");
  5. var inputs = $("#" + id + " :input").not(':button, :submit, :reset');
  6. $.each(inputs, function(i, n) {
  7. var caption = $(this).attr("caption");
  8.  
  9. var maxLength = $(this).attr("maxlength");
  10. if (isEmptyString(maxLength) == false) {
  11. var val = "";
  12. if ($(this).is("input")) {
  13. val = $(this).val();
  14. } else if ($(this).is("textarea")) {
  15. val = $(this).html();
  16. }
  17. if (val.length > parseInt(maxLength)) {
  18. if (funMsg != undefined) {
  19. funMsg(caption + "输入长度超过限制");
  20. }
  21. rtn = false;
  22. }
  23. }
  24. var notNull = $(this).attr("notnull");
  25. if (notNull == "true") {
  26. var val = "";
  27. if ($(this).is("input")) {
  28. val = $(this).val();
  29. } else if ($(this).is("textarea")) {
  30. val = $(this).html;
  31. } else if ($(this).is("select")) {
  32. val = $(this).val();
  33. }
  34. if (val.length == 0) {
  35. if (funMsg != undefined) {
  36. funMsg("请填写" + caption);
  37. }
  38. rtn = false;
  39. }
  40. }
  41. var chkEmail = $(this).attr("isemail");
  42. if (chkEmail == "true") {
  43. var val = "";
  44. if ($(this).is("input")) {
  45. val = $(this).val();
  46. } else if ($(this).is("textarea")) {
  47. val = $(this).html;
  48. }
  49. if (isEmail(val) == false) {
  50. if (funMsg != undefined) {
  51. funMsg("您填写的" + caption + "不是有效的邮箱格式,请重新填写");
  52. }
  53. rtn = false;
  54. }
  55. }
  56. var chkNum = $(this).attr("isint");
  57. if (chkNum == "true") {
  58. val = $(this).val();
  59. if (isInt(val) == false) {
  60. if (funMsg != undefined) {
  61. funMsg("您填写的" + caption + "不是有效的数字格式,请重新填写");
  62. }
  63. rtn = false;
  64. }
  65. }
  66. var chkFloat = $(this).attr("isfloat");
  67. if (chkFloat == "true") {
  68. val = $(this).val();
  69. if (isFloat(val) == false) {
  70. if (funMsg != undefined) {
  71. funMsg("您填写的" + caption + "不是有效的数字格式,请重新填写");
  72. }
  73. rtn = false;
  74. }
  75. }
  76. });
  77. return rtn;
  78. }

6.3 清除表单

  1. $.fn.formClear = function() {
  2. var id = $(this).attr("id");
  3. var inputs = $("#" + id + " :input").not(':button, :submit, :reset');
  4. $.each(inputs, function(i, n) {
  5. if ($(this).is("input")) {
  6. if ($(this).attr("type") == "text" || $(this).attr("type") == "hidden" || $(this).attr("type") == undefined) {
  7. $(this).val("");
  8. } else if ($(this).attr("type") == "checkbox") {
  9. $(this).prop("checked", false);
  10. } else if ($(this).attr("type") == "radio") {
  11. $(this).prop("checked", false);
  12. } else if ($(this).attr("type") == "password") {
  13. $(this).val("");
  14. }
  15. } else if ($(this).is("textarea")) {
  16. $(this).html("");
  17. } else if ($(this).is("select")) {
  18. var nid = $(this).attr("id");
  19. $("#" + nid + " option:first").attr("selected", "selected");
  20. }
  21. })
  22. }

6.4 填充表单

  1. $.fn.formFill = function(data) {
  2. var id = $(this).attr("id");
  3. var inputs = $("#" + id + " :input").not(':button, :submit, :reset');
  4. $.each(inputs, function(i, n) {
  5. var attr = $(this).attr("name");
  6. var valStr = getObjectData(data, attr);
  7. if (valStr == null || valStr == undefined) {
  8. valStr = "";
  9. } else {
  10. valStr = valStr.toString();
  11. }
  12. if ($(this).is("input")) {
  13. if ($(this).attr("type") == "text" || $(this).attr("type") == "hidden" || $(this).attr("type") == undefined) {
  14. $(this).val(valStr);
  15. } else if ($(this).attr("type") == "checkbox") {
  16. $(this).prop("checked", valStr);
  17. } else if ($(this).attr("type") == "radio") {
  18. $(this).prop("checked", valStr);
  19. } else if ($(this).attr("type") == "password") {
  20. $(this).val(valStr);
  21. }
  22. } else if ($(this).is("textarea")) {
  23. $(this).html(valStr);
  24. } else if ($(this).is("select")) {
  25. $(this).val(valStr);
  26. }
  27. })
  28. }

7. css中px的数值操作

7.1 获取css中px的数值

  1. $.fn.getCssPx = function(cssProp) {
  2. var cssStr = $(this).css(cssProp);
  3. cssStr = cssStr.substr(0, cssStr.length - 2);
  4. return parseInt(cssStr);
  5. }

7.2 设定css中px的数值

  1. $.fn.setCssPx = function(cssProp, value) {
  2. $(this).css(cssProp, value + "px");
  3. }

8. 未完待续

JS/JQUERY函数库的更多相关文章

  1. 前端之jquery函数库

    jquery介绍 jQuery是目前使用最广泛的javascript函数库.据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库.微软公司甚至把jQuery作为他们的官方库. ...

  2. JQuery函数库

    核心Core 函数$()动态创建由 jQuery 对象包装的 DOM 元素$.unique()去重排序函数$.inArray()在数组中搜索指定的值并返回其索引$.merge()合并数组 属性Para ...

  3. js jquery 函数回调

    JS 函数回调 $('#btn_update').click(function () { var table_id = $table.bootstrapTable('getSelections')[0 ...

  4. js jQuery函数 $.ajax()

    $.ajax() //$表示是jQuery cache:  要求为Boolean类型的参数,默认为true(当dataType为script时,默认为false),设置为false将不会从浏览器缓存中 ...

  5. 转:common.js 常用js公共函数库

    转自其他博主,自己开发备用 var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data: data, d ...

  6. css,js,jquery的载入方式和属性控制

    本文章主要总结了css,js,jQuery在编写程序时的载入方式,与属性控制方式html和css共同组成了一个最基础的网页,js为标签样式提供动态效果 一,css的载入方式与属性控制 1.1,css引 ...

  7. 在easyUI开发中,出现jquery.easyui.min.js函数库问题

    easyUI是jquery的一个插件,是民间的插件.easyUI使用起来很方便,里面有网页制作的最重要的三大方块:javascript代码.html代码和Css样式.我们在导入easyUI库后,可以直 ...

  8. Underscore——JS函数库

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826065.html underscore是什么——它是一个js函数库 jQuery统一了不同浏览器之间的 ...

  9. js jquery版本的 金额千分位转换函数(非正则,效率极高)

    没想到js里面没有 金额千分位格式化的处理函数(例:1,234.01 这样的格式),网上搜了一圈,都是使用正则的方式处理的.正则的效率不敢恭维啊,又耗费资源速度又慢(虽然处理起来会直观一些). 因此专 ...

随机推荐

  1. 006、Java中定义中文变量中文标识符

    01.代码如下 package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  2. Spring MVC RedirectAttributes取值方法

    RedirectAttributes是Spring mvc 3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的他有两种带参的方式:第一种: attr.addAttribute(" ...

  3. Codeforces Forethought Future Cup Elimination Round 选做

    1146C Tree Diameter 题意 交互题.有一棵 \(n(n\le 100)\) 个点的树,你可以进行不超过 \(9\) 次询问,每次询问两个点集中两个不在同一点集的点的最大距离.求树的直 ...

  4. SpringBoot-集成通用mapper

    SpringBoot-集成通用mapper SpringBoot-集成通用mapper ​ 我们在SpringBoot中整合了MyBatis,但是大量重复的增删改查还是很头疼的问题,MyBatis也给 ...

  5. Spring AOP 管理事务

    <aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* ...

  6. Centos 8双网卡设置

    原理:不管开发板是通过直连.路由器还是交换机连接到PC机,最终都是接到PC的以太网网卡(对笔记本来说,一般存在两个网卡,一个WIFI网卡和以太网网卡):因此要实现PC机与虚拟机的互ping,必须把虚拟 ...

  7. 5款国内免费CDN服务商及使用点评

    第一款,百度加速乐 加速乐目前被百度收购,这样百度也有了自己运营的CDN产品,可以丰富自身站长平台工具使用用户群.目前有免费用户和付费用户的区别,对于一般的网站免费方案也足够使用.特点具备智能解析.加 ...

  8. code first网站发布后数据表中没有数据问题

    code first网站发布后数据表中没有数据问题 (1).将internal sealed class Configuration类访问修饰符改为public  class Configuratio ...

  9. Redis 详解 (二) redis的配置文件介绍

    目录 1.开头说明 2.INCLUDES 3.MODULES 4.NETWORK 5.GENERAL 6.SNAPSHOTTING 7.REPLICATION 8.SECURITY 9.CLIENTS ...

  10. 读取docx表格中的信息

    参考了 http://blog.csdn.net/qq_34475777/article/details/62055523 http://www.cnblogs.com/deepwaterplan/a ...