首页 > Web开发 > 详细

jquery小知识点

时间:2014-07-22 23:16:22      阅读:506      评论:0      收藏:0      [点我收藏+]

一直用下边方法判断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")) {
    //...
}

 

Javascript Date对象格式化

 

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 {
    //
}

jquery小知识点

原文:http://www.cnblogs.com/tianboblog/p/3514173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!