How to find if element with specific id exists or not

前端 未结 6 1924
独厮守ぢ
独厮守ぢ 2020-12-08 04:18

In my JavaScript I want to check whether the element with specific id is exist or not, I tried it with 2 ways

1).

var myEle = document.getElementByI         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 05:08

    Use typeof for elements checks.

    if(typeof(element) === 'undefined')
    {
    // then field does not exist
    }
    

提交回复
热议问题