Javascript contains Method

Source: Internet
Author: User

there are many useful methods for IE, which were later copied by other browsers, such as the contains method. If element a contains Element B, true is returned; otherwise, false is returned. The only thing that does not support this method is the opposite of IE Firefox.

<br/> <! Doctype HTML> <br/> <title> Dom contains method by situ zhengmei </title> <br/> <meta charset = "UTF-8"/> <br/> <meta name = "keywords" content = "dom contains method by situ zhengmei"/> <br/> <meta name = "Description" content = "dom contains method by situ zhengmei"/> </P> <p> <SCRIPT type = "text/JavaScript"> <br/> window. onload = function () {<br/> var A = document. getelementbyid ('parent'), <br/> B = document. getelementbyid ('child '); <br/> alert (. cont Ains (B); <br/> alert (B. contains (a); <br/>}< br/> </SCRIPT> <br/> <H2 style = "text-align: center "> contains method </H2> </P> <p> <Div id =" parent "> <br/> <p> <br/> <strong id =" child "> in this example, an error is reported in Firefox. </Strong> <br/> </P> <br/> </div> <br/>

RunCode

However, Firefox supports the comparedocumentposition () method, which is developed by W3C and supported by standard browsers. However, it is not practical, so no one can use it and cannot be promoted. Its usage is similar to that of contains, but the returned value is not a Boolean value, but a very strange value. It is calculated in the following way:

bits Number meaning
000000 0 consistent element
000001 1 the node is in different documents (or one is out of the document)
000010 2 node B is before node A
000100 4 node A is before Node B
001000 8 node B contains node A
010000 16 node A contains Node B
100000 32 private use of the browser

<Br/> <! Doctype HTML> <br/> <title> Dom contains method by situ zhengmei </title> <br/> <meta charset = "UTF-8"/> <br/> <meta name = "keywords" content = "dom contains method by situ zhengmei"/> <br/> <meta name = "Description" content = "dom contains method by situ zhengmei"/> </P> <p> <SCRIPT type = "text/JavaScript"> </P> <p> window. onload = function () {<br/> var A = document. getelementbyid ('parent'), <br/> B = document. getelementbyid ('child '); <br/> alert (. compa Redocumentposition (B); // B is not connected to a, B is behind a, and B is contained by a 4 + 16 = 20 <br/> alert (B. comparedocumentposition (a); // A and B are not connected, A is in front of B, A contains B 2 + 8 = 10 <br/>}< br/> </SCRIPT> <br/> <H2 style = "text-align: center "> comparedocumentposition method </H2> </P> <p> <Div id =" parent "> <br/> <p> <br/> <strong id =" child "> This example runs in a standard browser. </Strong> <br/> </P> <br/> </div> <br/>

Run code

PPK provides the following solutions.

 
If (window. node & node. Prototype &&! Node. Prototype. Contains) {node. Prototype. Contains = function (ARG) {return !! (This. comparedocumentposition (ARG) & 16 )}}

I made a shorter one:

 
If (!! Window. Find) {htmlelement. Prototype. Contains = function (B) {return this. comparedocumentposition (B)-19> 0 }}

<Br/> <! Doctype HTML> <br/> <title> Dom contains method by situ zhengmei </title> <br/> <meta charset = "UTF-8"/> <br/> <meta name = "keywords" content = "dom contains method by situ zhengmei"/> <br/> <meta name = "Description" content = "dom contains method by situ zhengmei"/> </P> <p> <SCRIPT type = "text/JavaScript"> <br/> If (!! Window. find) {<br/> htmlelement. prototype. contains = function (B) {<br/> return this. comparedocumentposition (B)-19> 0 <br/>}< br/> window. onload = function () {<br/> var A = document. getelementbyid ('parent'), <br/> B = document. getelementbyid ('child '); <br/> alert (. contains (B); <br/> alert (B. contains (a); <br/>}< br/> </SCRIPT> <br/> <H2 style = "text-align: center "> contains method </H2> </P> <p> <Div id =" parent "> <br/> <p> <br/> <strong id =" child "> contains method </strong> <br/> </P> <br/> </div> <br/>

Run code

 
// 2011.9.24 by situ zhengmei var contains = function (root, El) {If (root. comparedocumentposition) return root = El | !! (Root. comparedocumentposition (EL) & 16); If (root. Contains & El. nodetype = 1) {return root. Contains (EL) & root! = El;} while (El = El. parentnode) if (El = root) return true; return false ;}
 // 2013.1.24 by situ zhengmei var contains = function (a, B, itself) {// whether the first node contains the second node // contains method support: chrome + firefox9 + ie5 +, opera9.64 + (estimated from 9.0 + ), safari5.1.7 + If (itself & A = B) {return true} if (. contains) {if (. nodetype = 9) return true; return. contains (B);} else if (. comparedocumentposition) {return !! (. Comparedocumentposition (B) & 16);} while (B = B. parentnode) if (a = B) Return true; return false ;}, 
Related Article

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.