Check if a div does NOT exist with javascript

前端 未结 9 2191
野性不改
野性不改 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:28

    Try getting the element with the ID and check if the return value is null:

    document.getElementById('some_nonexistent_id') === null
    

    If you're using jQuery, you can do:

    $('#some_nonexistent_id').length === 0
    

提交回复
热议问题