How to check null objects in jQuery

后端 未结 13 851
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:00

    Calling length property on undefined or a null object will cause IE and webkit browsers to fail!

    Instead try this:

    if($("#something") !== null){
      // do something
    }
    

    or

    if($("#something") === null){
      // don't do something
    }
    
    0 讨论(0)
  • 2020-11-30 18:04

    when the object is empty return this error:

    Uncaught TypeError: Cannot read property '0' of null
    

    I try this code :

    try{
      if ($("#btext" + i).length) {};               
    }catch(err){
      if ($("#btext" + i).length) {
         //working this code if the item, not NULL 
       }
    }
    
    0 讨论(0)
  • 2020-11-30 18:05

    Check the jQuery FAQ...

    You can use the length property of the jQuery collection returned by your selector:

    if ( $('#myDiv').length ){}
    
    0 讨论(0)
  • 2020-11-30 18:12

    (Since I don't seem to have enough reputation to vote down the answer...)

    Wolf wrote:

    Calling length property on undefined or a null object will cause IE and webkit browsers to fail!

    Instead try this:

     // NOTE!! THE FOLLOWING IS WRONG; DO NOT USE!  -- EleotleCram
    if($("#something") !== null){
      // do something
    }
    

    or

     // NOTE!! THE FOLLOWING IS WRONG; DO NOT USE!  -- EleotleCram
    if($("#something") === null){
      // don't do something
    }
    

    While it is true that calling the length property on an undefined or null object will cause browsers to fail, the result of jQuery's selectors (the $('...')) will never be null or undefined. Thus the code suggestions make no sense. Use one of the other answers, they make more sense.


    (Update 2012) Because people look at code and this answer is pretty high up the list: For the last couple of years, I have been using this small plugin:

      jQuery.fn['any'] = function() {
         return (this.length > 0);
      };
    

    I think $('div').any() reads better than $('div').length, plus you won't suffer as much from typos: $('div').ayn() will give a runtime error, $('div').lenght will silently most likely always be falsy.

    __
    Edits november 2012:

    1) Because people tend to look at code and not read what is said around the code, I added two big caveat lector notes to the quoted code of Wolf.
    2) I added code of the small plugin I use for this situation.

    0 讨论(0)
  • 2020-11-30 18:13

    jquery $() function always return non null value - mean elements matched you selector cretaria. If the element was not found it will return an empty array. So your code will look something like this -

    if ($("#btext" + i).length){
            //alert($("#btext" + i).text());
        $("#btext" + i).text("Branch " + i);
    }
    
    0 讨论(0)
  • 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.
    
    0 讨论(0)
提交回复
热议问题