The technique of realizing code _javascript by JS contains method

Source: Internet
Author: User
In order to be compatible with IE and FF we have to use the following methods:
<textarea id="runcode24456"><!doctype html> <title>dom contains method by Masaki </title> <meta charset= "utf-8"/> <meta na Me= "keywords" content= dom contains method by Masaki/> <meta name= "description" content= "Dom contains method by Masaki"/> <script type= "Text/javascript" > if (!! Window.find) {HTMLElement.prototype.contains = function (b) {return this.comparedocumentposition (b)-> 0}} WI Ndow.onload = function () {var A = document.getElementById (' parent '), B = document.getElementById (' child '); Alert (A.contains (B)); Alert (B.contains (A)); } </script> <p >contains method </p> <div id= "parent" > <p> <strong id= "Child" >conta Ins method </strong> </p> </div></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

However, Firefox supports the Comparedocumentposition () method, which is the method developed by the Web, standard browsers support, but the practicality is very poor, so no one to use, not spread. It uses the same form as contains, but returns not a Boolean, but a very strange number, which is summed up in the following way:
Bits number meaning
000000 0 element consistent
000001 1 node in a different document (or one outside the document)
000010 2 node B before node A
000100 4 Node A in Before Node B
001000 8 Node B contains node A
010000 node A contains node B
100000 Private use of browser
<textarea id="runcode16170"><!doctype html> <title>dom contains method by Masaki </title> <meta charset= "Utf-8"/> <meta N Ame= "keywords" content= dom contains method by Masaki/> <meta name= "description" content= "Dom contains method by Masaki"/> <script type= "Text/javascript" > window.onload = function () {var A = document.getElementById (' parent '), B = doc Ument.getelementbyid (' child '); Alert (A.comparedocumentposition (B));//b is not connected to a, b after a, B is contained 4+16 = alert (B.comparedocumentposition (a)),//a is not connected to B, A in front of B, A contains b 2+8 = ten} </script> <p >comparedocumentposition method </p> <div id= "parent" > <p > <strong id= "Child" > This example please run in a standard browser. </strong> </p> </div></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


PPK gives the following solutions.
Copy Code code as follows:

if (window. Node && node.prototype &&! Node.prototype.contains) {
Node.prototype.contains = function (ARG) {
Return!! (This.comparedocumentposition (ARG) & 16)
}
}

I got a shorter one:
Copy Code code as follows:

if (!! Window.find) {
HTMLElement.prototype.contains = function (B) {
Return This.comparedocumentposition (B)-> 0
}
}

<textarea id="runcode88957"><!doctype html> <title>dom contains method by Masaki </title> <meta charset= "Utf-8"/> <meta N Ame= "keywords" content= dom contains method by Masaki/> <meta name= "description" content= "Dom contains method by Masaki"/> <script type= "Text/javascript" > if (!! Window.find) {HTMLElement.prototype.contains = function (b) {return this.comparedocumentposition (b)-> 0}} WI Ndow.onload = function () {var A = document.getElementById (' parent '), B = document.getElementById (' child '); Alert (A.contains (B)); Alert (B.contains (A)); } </script> <p >contains method </p> <div id= "parent" > <p> <strong id= "Child" >contai ns method </strong> </p> </div></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Copy Code code as follows:

2011.9.24 by Masaki
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;
}

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.