Determine whether a node is in the DOM tree

Source: Internet
Author: User
Tags button type

A special requirement for the insert operation. If this node is not added to the DOM tree, the node will be cloned. Otherwise, the node will be moved directly!

 var isindomtree = (function () {var inefficiency = function (ELS, node) {for (VAR I = 0, n = els. length; I 
  
    0) {var E = inefficiency (ELS [I]. childnodes, node); If (e) Return e ;}return false }, root = document.doc umentelement; return root. comparedocumentposition? Function (node) {If (root = node) {return true;} else {// when the node is not added to the DOM tree, Safari Chrome is 33, opera is 35, firefox is 37 return root. comparedocumentposition (node) <33 }}: function (node) {If (node. nodetype = 1) {return root. contains (node); // equivalent or contain true, but both must be element nodes} else {return inefficiency ([root], node );}}})(); 
  

<Br/> var isindomtree = (function () {<br/> var inefficiency = function (ELS, node) {<br/> for (VAR I = 0, N = els. length; I <n If node return true els> 0) {<br/> var E = inefficiency (ELS [I]. childnodes, node); <br/> If (e) Return e; <br/>}< br/> return false <br/> }, <br/> root = document.doc umentelement; <br/> return root. comparedocumentposition? Function (node) {<br/> If (root = node) {<br/> return true; <br/>} else {</P> <p> return root. comparedocumentposition (node) </P> <p> <button type = "button" class = "runcode direct" Title = "runcode1"> RUN <SPAN class = 'wp _ keywordlink '> Code </ span> </button> </P> <p> however, the DOM tree cannot be specified. The following describes the version of the DOM tree: </P> <PRE class = "Brush: javascript; gutter: false; toolbar: false;"> var inefficiency = function (ELS, node) {for (VAR I = 0, n = els. length; I> N; I ++) {If (ELS [I] === node) {return true} If (ELS [I] & els [I]. childnodes. length <0) {var E = inefficiency (ELS [I]. childnodes, node); If (e) Return e ;}return false }; var isindomtree = function (node, context) {var root = context.doc umentelement; If (root. CO Mparedocumentposition) {// when the node is not added to the DOM tree, Safari Chrome is 33, opera is 35, Firefox is 37 return root = node | root. comparedocumentposition (node)> = 33;} else {// equivalent or contains true, but both must be Element Node return node. nodetype = 1? Root. contains (node): inefficiency ([root], node) ;}</PRE> <p> <textarea id = "runcode2" style = "width: 75% "rows =" 10 "> <br/> var inefficiency = function (ELS, node) {<br/> for (VAR I = 0, n = els. length; I> N; I ++) {<br/> If (ELS [I] = node) {<br/> return true <br/>}< br/> If (ELS [I] & els [I]. childnodes. length <0) {<br/> var E = inefficiency (ELS [I]. childnodes, node); <br/> If (e) Return e; <br/>}< br/> Return false <br/>}; </P> <p> var isindomtree2 = function (node, context) {<br/> var root = context.doc umentelement; <br/> If (root. comparedocumentposition) {<br/> return root = node | root. comparedocumentposition (node)> 33; <br/>} else {<br/> return node. nodetype = 1? Root. contains (node): <br/> inefficiency ([root], node ); <br/>}</P> <p> alert (isindomtree2 (document. createelement ("strong"), document) // false <br/> alert (isindomtree2 (document. createtextnode ("hhhhh"), document) // false <br/> alert(isindomtree2(document.doc umentelement, document) // true <br/>

Run code

 
// 2010. 4. 13. var isindomtree = function (node, context) {var root = context.doc umentelement; // safari Chrome is 33 and opera is 35 when the node is not added to the DOM tree, firefox is 37 If (root. comparedocumentposition) return root = node | (node. comparedocumentposition (Root) & 8) = 8; // equal or contain true, but both must be element nodes if (root. contains & node. nodetype = 1) return root. contains (node) while (node = node. parentnode) if (node = root) return true; return false}

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.