How do I check if an element is undefined?

This question already has an answer here:

  • Detecting an undefined object property 40 answers
  • How to determine if variable is 'undefined' or 'null'? 24 answers

  • Using typeof :

    if (typeof marcamillion == 'undefined') {
        console.log("Marcamillion is an undefined variable.");
    }
    

    Edit for the second question:

    if ($(this).attr('value')) {
        // code
    }
    else {
        console.log('nope.')
    }
    

    if (typeof marcamillion === 'undefined') {
        console.log("Marcamillion is an undefined variable.");
    }
    

    请注意,使用===而不是==被认为是更好的风格。

    链接地址: http://www.djcxy.com/p/22636.html

    上一篇: 在if语句中防止错误“未定义”

    下一篇: 如何检查元素是否未定义?