How to check null objects in jQuery

后端 未结 13 889
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

    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);
    }
    

提交回复
热议问题