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

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

    A absolute right method, check target is a real html element primary code:

        (function (scope) {
            if (!scope.window) {//May not run in window scope
                return;
            }
            var HTMLElement = window.HTMLElement || window.Element|| function() {};
    
            var tempDiv = document.createElement("div");
            var isChildOf = function(target, parent) {
    
                if (!target) {
                    return false;
                }
                if (parent == null) {
                    parent = document.body;
                }
                if (target === parent) {
                    return true;
                }
                var newParent = target.parentNode || target.parentElement;
                if (!newParent) {
                    return false;
                }
                return isChildOf(newParent, parent);
            }
            /**
             * The dom helper
             */
            var Dom = {
                /**
                 * Detect if target element is child element of parent
                 * @param {} target The target html node
                 * @param {} parent The the parent to check
                 * @returns {} 
                 */
                IsChildOf: function (target, parent) {
                    return isChildOf(target, parent);
                },
                /**
                 * Detect target is html element
                 * @param {} target The target to check
                 * @returns {} True if target is html node
                 */
                IsHtmlElement: function (target) {
                    if (!X.Dom.IsHtmlNode(target)) {
                        return false;
                    }
                    return target.nodeType === 1;
                },
                /**
                 * Detect target is html node
                 * @param {} target The target to check
                 * @returns {} True if target is html node
                 */
                IsHtmlNode:function(target) {
                    if (target instanceof HTMLElement) {
                        return true;
                    }
                    if (target != null) {
                        if (isChildOf(target, document.documentElement)) {
                            return true;
                        }
                        try {
                            tempDiv.appendChild(target.cloneNode(false));
                            if (tempDiv.childNodes.length > 0) {
                                tempDiv.innerHTML = "";
                                return true;
                            }
                        } catch (e) {
    
                        }
                    }
                    return false;
                }
            };
            X.Dom = Dom;
        })(this);
    

提交回复
热议问题