简体   繁体   中英

Typeof === 'undefined' issue

Newbie here trying to work on this function which is supposed to console.log "I am undefined" if the contents of href is blank. I would appreciate any help as I am VERY stuck... :-(

$(document).ready(function () {
    $('.facebook').each(function () {
        var facebook = $(this).attr('href')
        if (typeof facebook === "undefined") {
            console.log("I am undefined");
        }
    });
});

It won't be undefined, I think you can probably use :-

if (!facebook) {
    console.log("I am undefined");
}

it will take care of any falsy value as null , undefined , "" .

.attr return plain object . it won't return undefined

Try like this

$(document).ready(function () {
    $('.facebook').each(function () {
        var facebook = $(this).attr('href')
        if (!facebook) {
            console.log("I am undefined");
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM