dom-determining element node types

Source: Internet
Author: User

http://stackoverflow.com/questions/384286/ javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object/27112297#27112297

To determine the node type:

<Body>    <DivID= "Test">Aaa</Div>    <!--This is a comment node -    <Script>        variselement= function(EL) {return !!El&&El.nodetype=== 1;//pay attention to this return and!! El's Judgment        }        vara={nodeType:1} console.log (Iselement (document.getElementById ("Test"))); Console.log (Iselement (document.getElementById ("Test"). nextSibling));    Console.log (Iselement (a)); </Script></Body>

A more rigorous approach to judgment

<Body>    <DivID= "Test">Aaa</Div>    <!--This is a comment node -    <Script>        varTestdiv=Document.createelement ('Div'); variselement= function(obj) {if(obj&&Obj.nodetype=== 1) {//First, filter the simplest                if(window. Node&&(objinstanceofNode)) { //if it is IE9, determine if it is an instance of node                    return true; //because obj may be from another document object, it cannot easily return false                }                Try {//Finally, this is a very poor efficiency, but certainly feasible scheme to determinetestdiv.appendchild (obj);                Testdiv.removechild (obj); } Catch(e) {return false; }                return true; }            return false; }        vara={nodeType:1} console.log (Iselement (document.getElementById ("Test"))); Console.log (Iselement (document.getElementById ("Test"). nextSibling));    Console.log (Iselement (a)); </Script></Body>

dom-determining element node types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.