Check that element is in DOM or not with jQuery?

前端 未结 3 428
野性不改
野性不改 2020-12-21 05:22

I have quick and simple question. How can I check that an element is in the DOM or not?

For example how can I check if there is

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-21 05:47

    here are three different ways:

    if ($("#does_i_really_exist_or").length > 0) {
    
       // do something
    }
    
    if ($("#does_i_really_exist_or").size() > 0) {
    
       // do something
    }
    
    if ($("#does_i_really_exist_or")[0]) {
    
       // do something
    }
    

提交回复
热议问题