How do you check if a JavaScript Object is a DOM Object?

后端 未结 30 2791
-上瘾入骨i
-上瘾入骨i 2020-11-22 16:06

I\'m trying to get:

document.createElement(\'div\')  //=> true
{tagName: \'foobar something\'}  //=> false

In my own scripts, I used

30条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 16:31

    I think that what you have to do is make a thorough check of some properties that will always be in a dom element, but their combination won't most likely be in another object, like so:

    var isDom = function (inp) {
        return inp && inp.tagName && inp.nodeName && inp.ownerDocument && inp.removeAttribute;
    };
    

提交回复
热议问题