jquery小知识点 Javascript Date对象格式化 jQuery判断对象是否存在
一直用下边方法判断checkbox是否选中:
if ($(elem).attr("checked") == true) { //... }
从1.6开始,attr方法获取checkbox的checked属性值只有undefined和checked,所以,从1.6开始判断checkbox是否选中要用以下方法:
if ($(elem).attr("checked") == "checked") { //... } //or if ($(elem).prop("checked") == true) { //... }
其实,我们还有其他方法来判断checkbox是否选中,以下方法在jQuery各版本都适用,推荐使用:
if ($(elem).is(":checked")) { //... }
Date.prototype.format = function (fmt) { var o = { "y+": this.getFullYear(), //年 "M+": this.getMonth() + 1, //月 "d+": this.getDate(), //日 "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时(12小时制) "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; var week = { "0": "日", "1": "一", "2": "二", "3": "三", "4": "四", "5": "五", "6": "六" }; if (arguments.length == 0) { fmt = "yyyy-MM-dd HH:mm:ss"; } if (/(E+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "星期" : "周") : "") + week[this.getDay() + ""]); } 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(-1 * RegExp.$1.length))); } } return fmt; }
调用示例:
document.write(new Date().format()); //2013-07-04 22:11:57 document.write(new Date().format("yyyy-MM-dd E HH:mm:ss")); //2013-07-04 四 22:11:57 document.write(new Date().format("yyyy-MM-dd EE HH:mm:ss")); //2013-07-04 周四 22:11:57 document.write(new Date().format("yyyy-MM-dd EEE HH:mm:ss")); //2013-07-04 星期四 22:11:57 document.write(new Date().format("yy-M-d H:m:s.S")); //13-7-4 22:11:57.265
jQuery判断对象是否存在
受js影响,在jq中我习惯性用下边语句判断对象是否存在:
程序代码
if ($("#eid")) { // } else { // }
实际上,无论对象是否存在,$("#eid")的结果都是一个object,所以$("#eid")==true,正确的判断方法应是:
程序代码
if ($("#eid").length > 0) { // } else { // }
Javascript是很富有艺术的语言;平时工作当中也研究过一些插件,想想那些前端大牛们,真是让人不得不佩服啊!js确实挺强大,在接下来的日子多多学习吧!