Check if a div does NOT exist with javascript

前端 未结 9 2164
野性不改
野性不改 2020-12-12 16:42

Checking if a div exists is fairly simple

if(document.getif(document.getElementById(\'if\')){

}

But how can I check if a div with the give

相关标签:
9条回答
  • 2020-12-12 17:40
    var myElem = document.getElementById('myElementId');
    if (myElem === null) alert('does not exist!');
    
    0 讨论(0)
  • 2020-12-12 17:42

    Check both my JavaScript and JQuery code :

    JavaScript:

    if (!document.getElementById('MyElementId')){
        alert('Does not exist!');
    }
    

    JQuery:

    if (!$("#MyElementId").length){
        alert('Does not exist!');
    }
    
    0 讨论(0)
  • 2020-12-12 17:45

    That works with :

     var element = document.getElementById('myElem');
     if (typeof (element) != undefined && typeof (element) != null && typeof (element) != 'undefined') {
         console.log('element exists');
     }
     else{
         console.log('element NOT exists');
     }
    
    0 讨论(0)
提交回复
热议问题