Check that element is in DOM or not with jQuery?

前端 未结 3 419
野性不改
野性不改 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
    }
    
    0 讨论(0)
  • 2020-12-21 05:53
    if ($("#does_i_really_exist_or").length > 0) {
    
       // do something
    }
    
    0 讨论(0)
  • 2020-12-21 05:57
    if ( $("#does_i_really_exist_or").length ) {
       //-- It does exist.
    }
    
    0 讨论(0)
提交回复
热议问题