document.getelementbyId will return null if element is not defined?

前端 未结 3 888

In my code, I see this:

if (document.getElementById(\'xx\') !=null) {
    //do stuff
}

if xx element is not defined, will this

3条回答
  •  借酒劲吻你
    2020-12-29 19:28

    getElementById is defined by DOM Level 1 HTML to return null in the case no element is matched.

    !==null is the most explicit form of the check, and probably the best, but there is no non-null falsy value that getElementById can return - you can only get null or an always-truthy Element object. So there's no practical difference here between !==null, !=null or the looser if (document.getElementById('xx')).

提交回复
热议问题