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

后端 未结 30 2531
-上瘾入骨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:41

    I think prototyping is not a very good solution but maybe this is the fastest one: Define this code block;

    Element.prototype.isDomElement = true;
    HTMLElement.prototype.isDomElement = true;
    

    than check your objects isDomElement property:

    if(a.isDomElement){}
    

    I hope this helps.

提交回复
热议问题