【问题标题】:Typeof === 'undefined' issueTypeof === '未定义' 问题
【发布时间】:2015-05-06 07:57:01
【问题描述】:

新手在这里尝试使用这个功能,如果href的内容为空白,则应该控制台记录“我未定义”。我非常感谢任何帮助... :-(

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

【问题讨论】:

  • 你的 facebook 变量是 null 不是未定义。检查 null 不是未定义。
  • 使用 alert 或 console.log(facebook) 检查 facebook
  • 好的,谢谢,会试试的

标签: javascript jquery html undefined typeof


【解决方案1】:

它不会是未定义的,我想你可能可以使用:-

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

它将处理 nullundefined"" 等任何虚假值。

【讨论】:

    【解决方案2】:

    .attr 返回普通对象。它不会返回 undefined

    这样试试

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

    【讨论】:

      猜你喜欢
      • 2011-02-11
      • 1970-01-01
      • 2011-06-11
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      相关资源
      最近更新 更多