1. 判断字符串是否为空

function isEmptyString(str) {
return str == undefined || str == "" || str == null;
}

2. 判断数据格式

2.1 判断是否邮箱格式

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

2.2 判断是否整数格式

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

2.3 判断是否浮点数字格式

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

3. 获取object中属性的值

function getObjectData(object, field) {
var fieldArray = field.split(".");
var childObj = object;
$.each(fieldArray, function(i, n) {
childObj = childObj[n];
})
return childObj;
}

例如:

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

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

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

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

Date.prototype.Format = function(fmt) {
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;
}

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

function setGloableKeyPress(keyCode, onKeyPress) {
document.onkeydown = function(e) {
var ev = document.all ? window.event : e;
if (ev.keyCode == keyCode) {
onKeyPress();
}
}
}

6. form表单功能扩展

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

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

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

$.fn.formCheck = function(funMsg) {
var rtn = true; var id = $(this).attr("id");
var inputs = $("#" + id + " :input").not(':button, :submit, :reset');
$.each(inputs, function(i, n) {
var caption = $(this).attr("caption"); var maxLength = $(this).attr("maxlength");
if (isEmptyString(maxLength) == false) {
var val = "";
if ($(this).is("input")) {
val = $(this).val();
} else if ($(this).is("textarea")) {
val = $(this).html();
}
if (val.length > parseInt(maxLength)) {
if (funMsg != undefined) {
funMsg(caption + "输入长度超过限制");
}
rtn = false;
}
}
var notNull = $(this).attr("notnull");
if (notNull == "true") {
var val = "";
if ($(this).is("input")) {
val = $(this).val();
} else if ($(this).is("textarea")) {
val = $(this).html;
} else if ($(this).is("select")) {
val = $(this).val();
}
if (val.length == 0) {
if (funMsg != undefined) {
funMsg("请填写" + caption);
}
rtn = false;
}
}
var chkEmail = $(this).attr("isemail");
if (chkEmail == "true") {
var val = "";
if ($(this).is("input")) {
val = $(this).val();
} else if ($(this).is("textarea")) {
val = $(this).html;
}
if (isEmail(val) == false) {
if (funMsg != undefined) {
funMsg("您填写的" + caption + "不是有效的邮箱格式,请重新填写");
}
rtn = false;
}
}
var chkNum = $(this).attr("isint");
if (chkNum == "true") {
val = $(this).val();
if (isInt(val) == false) {
if (funMsg != undefined) {
funMsg("您填写的" + caption + "不是有效的数字格式,请重新填写");
}
rtn = false;
}
}
var chkFloat = $(this).attr("isfloat");
if (chkFloat == "true") {
val = $(this).val();
if (isFloat(val) == false) {
if (funMsg != undefined) {
funMsg("您填写的" + caption + "不是有效的数字格式,请重新填写");
}
rtn = false;
}
}
});
return rtn;
}

6.3 清除表单

$.fn.formClear = function() {
var id = $(this).attr("id");
var inputs = $("#" + id + " :input").not(':button, :submit, :reset');
$.each(inputs, function(i, n) {
if ($(this).is("input")) {
if ($(this).attr("type") == "text" || $(this).attr("type") == "hidden" || $(this).attr("type") == undefined) {
$(this).val("");
} else if ($(this).attr("type") == "checkbox") {
$(this).prop("checked", false);
} else if ($(this).attr("type") == "radio") {
$(this).prop("checked", false);
} else if ($(this).attr("type") == "password") {
$(this).val("");
}
} else if ($(this).is("textarea")) {
$(this).html("");
} else if ($(this).is("select")) {
var nid = $(this).attr("id");
$("#" + nid + " option:first").attr("selected", "selected");
}
})
}

6.4 填充表单

$.fn.formFill = function(data) {
var id = $(this).attr("id");
var inputs = $("#" + id + " :input").not(':button, :submit, :reset');
$.each(inputs, function(i, n) {
var attr = $(this).attr("name");
var valStr = getObjectData(data, attr);
if (valStr == null || valStr == undefined) {
valStr = "";
} else {
valStr = valStr.toString();
}
if ($(this).is("input")) {
if ($(this).attr("type") == "text" || $(this).attr("type") == "hidden" || $(this).attr("type") == undefined) {
$(this).val(valStr);
} else if ($(this).attr("type") == "checkbox") {
$(this).prop("checked", valStr);
} else if ($(this).attr("type") == "radio") {
$(this).prop("checked", valStr);
} else if ($(this).attr("type") == "password") {
$(this).val(valStr);
}
} else if ($(this).is("textarea")) {
$(this).html(valStr);
} else if ($(this).is("select")) {
$(this).val(valStr);
}
})
}

7. css中px的数值操作

7.1 获取css中px的数值

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

7.2 设定css中px的数值

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

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. py交易

    下载之后发现是pyc文件,反编译一下 网站:https://tool.lu/pyc/ 发现其实就是base64转换为16进制,然后减去16再和32异或,就可以得到答案了 nctf{d3c0mpil1n ...

  2. 027、MySQL字符串替换函数,文本替换函数,字符串填充函数

    #文本填充 ,'); #ABC12121212121212121 #文本替换 SELECT REPLACE('田攀520','攀','ABC'); #田ABC520 不忘初心,如果您认为这篇文章有价值 ...

  3. 05.Delphi接口的多重继承深入

    由于是IInterface,申明了SayHello,需要由继承类来实现函数,相对于03篇可以再精简一下 unit uSayHello; interface uses SysUtils, Windows ...

  4. eshop5-maven 安装

    1. Maven 安装 2.下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.0.5/binaries/ 3. 通过ta ...

  5. javascript判断数组是否包含了指定的元素

    jQuery写法: var arr = [ "xml", "html", "css", "js" ]; $.inArra ...

  6. 解析基于keras深度学习框架下yolov3的算法

    一.前言 由于前一段时间以及实现了基于keras深度学习框架下yolov3的算法,本来想趁着余热将自己的心得体会进行总结,但由于前几天有点事就没有完成计划,现在趁午休时间整理一下. 二.Keras框架 ...

  7. java 如何爬取百度百科词条内容(java如何使用webmagic爬取百度词条)

    这是老师所布置的作业 说一下我这里的爬去并非能把百度词条上的内容一字不漏的取下来(而是它分享链接的一个主要内容概括...)(他的主要内容我爬不到 也不想去研究大家有好办法可以call me) 例如 互 ...

  8. Golang的循环结构-for语句

    Golang的循环结构-for语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.for循环语法 循环结构在生活中的场景也非常的多,比如: ()上班族们每天朝九晚五的生活; ( ...

  9. Oracle 新建用户、赋予权限

    1.新建用户 新建用户: create User username Identified by password 修改用户密码: alter User username Identified by p ...

  10. 五十九、SAP中常用预定义系统变量

    一.SAP中常用预定义系统变量 内容如下: 二.系统变量定义在结构SYST里,我们打开SE38 三.在代码编辑器输入SYST变量 四.双击SYST,来到这个系统结构,里面有很多系统变量 五.我们随便写 ...