How to find with javascript if element exists in DOM or it's virtual (has been just created by createElement)

后端 未结 6 1516
庸人自扰
庸人自扰 2021-01-12 04:36

I\'m looking for a way to find if element referenced in javascript has been inserted in the document.

Lets illustrate a case with following code:

v         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 05:03

    Do this:

    var elem = document.createElement('div');
    elem.setAttribute('id', 'my_new_div');
    
    if (document.getElementById('my_new_div')) { } //element exists in the document.
    

提交回复
热议问题