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

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

    differentiate a raw js object from a HTMLElement

    function isDOM (x){
         return /HTML/.test( {}.toString.call(x) );
     }
    

    use:

    isDOM( {a:1} ) // false
    isDOM( document.body ) // true
    

    // OR

    Object.defineProperty(Object.prototype, "is",
        {
            value: function (x) {
                return {}.toString.call(this).indexOf(x) >= 0;
            }
        });
    

    use:

    o={}; o.is("HTML") // false o=document.body; o.is("HTML") // true

    0 讨论(0)
  • 2020-11-22 16:20

    Each DOMElement.constructor returns function HTML...Element() or [Object HTML...Element] so...

    function isDOM(getElem){
        if(getElem===null||typeof getElem==="undefined") return false;
        var c = getElem.constructor.toString();
        var html = c.search("HTML")!==-1;
        var element = c.search("Element")!==-1;
        return html&&element;
    }
    
    0 讨论(0)
  • 2020-11-22 16:21

    In Firefox, you can use the instanceof Node. That Node is defined in DOM1.

    But that is not that easy in IE.

    1. "instanceof ActiveXObject" only can tell that it is a native object.
    2. "typeof document.body.appendChild=='object'" tell that it may be DOM object, but also can be something else have same function.

    You can only ensure it is DOM element by using DOM function and catch if any exception. However, it may have side effect (e.g. change object internal state/performance/memory leak)

    0 讨论(0)
  • 2020-11-22 16:23
    var IsPlainObject = function ( obj ) { return obj instanceof Object && ! ( obj instanceof Function || obj.toString( ) !== '[object Object]' || obj.constructor.name !== 'Object' ); },
        IsDOMObject = function ( obj ) { return obj instanceof EventTarget; },
        IsDOMElement = function ( obj ) { return obj instanceof Node; },
        IsListObject = function ( obj ) { return obj instanceof Array || obj instanceof NodeList; },
    

    // In fact I am more likely t use these inline, but sometimes it is good to have these shortcuts for setup code

    0 讨论(0)
  • 2020-11-22 16:24

    You could try appending it to a real DOM node...

    function isDom(obj)
    {
        var elm = document.createElement('div');
        try
        {
            elm.appendChild(obj);
        }
        catch (e)
        {
            return false;
        }
    
        return true;
    }
    
    0 讨论(0)
  • 2020-11-22 16:24

    I suggest a simple way to testing if a variable is an DOM element

    function isDomEntity(entity) {
      if(typeof entity  === 'object' && entity.nodeType !== undefined){
         return true;
      }
      else{
         return false;
      }
    }
    

    or as HTMLGuy suggested:

    const isDomEntity = entity => {
      return typeof entity   === 'object' && entity.nodeType !== undefined
    }
    
    0 讨论(0)
提交回复
热议问题