How to check null objects in jQuery

后端 未结 13 882
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:17

    if ( $('#whatever')[0] ) {...}
    

    The jQuery object which is returned by all native jQuery methods is NOT an array, it is an object with many properties; one of them being a "length" property. You can also check for size() or get(0) or get() - 'get(0)' works the same as accessing the first element, i.e. $(elem)[0]

提交回复
热议问题