zoukankan      html  css  js  c++  java
  • 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")) {
        //...
    }

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

    Javascript是很富有艺术的语言;平时工作当中也研究过一些插件,想想那些前端大牛们,真是让人不得不佩服啊!js确实挺强大,在接下来的日子多多学习吧!

  • 相关阅读:
    配置secureCRT
    LINUX的网口绑定(bond)
    背包形动态规划 fjutoj2375 金明的预算方案
    背包形动态规划 fjutoj1380 Piggy-Bank
    背包形动态规划 fjutoj2347 采药
    树形动态规划 fjutoj-2392 聚会的快乐
    树形动态规划 fjutoj-2131 第四集,聚集城市
    andriod开发--使用Http的Get和Post方式与网络交互通信
    线段树复合标记
    图论之拓扑排序 poj 2367 Genealogical tree
  • 原文地址:https://www.cnblogs.com/tianboblog/p/3514173.html
Copyright © 2011-2022 走看看