How to check null objects in jQuery

后端 未结 13 862
Happy的楠姐
Happy的楠姐 2020-11-30 17:24

I\'m using jQuery and I want to check the existence of an element in my page. I have written following code, but it\'s not working:

if($(\"#btext\" + i) != n         


        
13条回答
  •  庸人自扰
    2020-11-30 18:13

    In jQuery 1.4 you get the $.isEmptyObject function, but if you are forced to use an older version of jQ like us poor Drupal developers just steal use this code:

    // This is a function similar to the jQuery 1.4 $.isEmptyObject.
    function isObjectEmpty(obj) {
      for ( var name in obj ) {
        return false;
      }
      return true;
    }
    

    Use it like:

    console.log(isObjectEmpty(the_object)); // Returns true or false.
    

提交回复
热议问题