判断value是否为空

function isEmpty(obj) { 
    if (obj == undefined || obj == null || new String(obj) == '') { 
        return true; 
    } else { 
        return false; 
    } 
}

判断是不是数字

//jQuery2.1
function isNumeric(obj) {
    return obj - parseFloat(obj) >= 0;
}

判断字符串是否是日期格式

function isDate(date) {
    return (new Date(date) !== "Invalid Date") && !isNaN(new Date(date));
}

or

function isDate(value) {  
    return Object.prototype.toString.call(value) === '[object Date]'  
} 

判断是不是数组类型的数据

function isArray(obj){
    return Object.prototype.toString.call(obj) === '[object Array]';
}

判断是不是函数

function isFunction(value) {  
    return Object.prototype.toString.call(value) === '[object Function]'  
} 

判断是不是DOM Element

function isElement(obj){
    try {
        //Using W3 DOM2 (works for FF, Opera and Chrome)
        return obj instanceof HTMLElement;
    }
    catch(e){
        //Browsers not supporting W3 DOM2 don't have HTMLElement and
        //an exception is thrown and we end up here. Testing some
        //properties that all elements have (works on IE7)
        return (typeof obj==="object") &&
            (obj.nodeType===1) && (typeof obj.style === "object") &&
            (typeof obj.ownerDocument ==="object");
    }
}
本文最后更新时间:2020年9月7日

标签: javascript, 判断, 类型

添加新评论