js contains方法實現代碼_javascript技巧

來源:互聯網
上載者:User
為了相容IE和FF我們不得不用以下方法: 
<!doctype html> <title>dom contains 方法 by 司徒正美</title> <meta charset="utf-8"/> <meta name="keywords" content="dom contains方法 by 司徒正美" /> <meta name="description" content="dom contains方法 by 司徒正美" /> <script type="text/javascript"> if(!!window.find){ HTMLElement.prototype.contains = function(B){ return this.compareDocumentPosition(B) - 19 > 0 } } window.onload = function(){ var A = document.getElementById('parent'), B = document.getElementById('child'); alert(A.contains(B)); alert(B.contains(A)); } </script> <h2 >contains方法</h2> <div id="parent"> <p> <strong id="child" >contains方法</strong> </p> </div>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

不過Firefox支援compareDocumentPosition() 方法,這是W3C制定的方法,標準瀏覽器都支援,不過實用性性很差,因此沒有什麼人用,推廣不開來。它的使用形式與contains差不多,但返回的不是一個布爾值,而是一個很奇怪的數值,它是通過如下方式累加計算出來的:
Bits Number Meaning
000000 0 元素一致
000001 1 節點在不同的文檔(或者一個在文檔之外)
000010 2 節點 B 在節點 A 之前
000100 4 節點 A 在節點 B 之前
001000 8 節點 B 包含節點 A
010000 16 節點 A 包含節點 B
100000 32 瀏覽器的私人使用
<!doctype html> <title>dom contains 方法 by 司徒正美</title> <meta charset="utf-8"/> <meta name="keywords" content="dom contains方法 by 司徒正美" /> <meta name="description" content="dom contains方法 by 司徒正美" /> <script type="text/javascript"> window.onload = function(){ var A = document.getElementById('parent'), B = document.getElementById('child'); alert(A.compareDocumentPosition(B));//B與A不相連,B在A的後面,B被A包含 4+16 = 20 alert(B.compareDocumentPosition(A));//A與B不相連,A在B的前面,A包含B 2+8 = 10 } </script> <h2 >compareDocumentPosition方法</h2> <div id="parent"> <p> <strong id="child" >本例子請在標準瀏覽器中運行。</strong> </p> </div>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]


PPK給出如下解決方案。
複製代碼 代碼如下:

if (window.Node && Node.prototype && !Node.prototype.contains){
Node.prototype.contains = function (arg) {
return !!(this.compareDocumentPosition(arg) & 16)
}
}

我搞出個更短的:
複製代碼 代碼如下:

if(!!window.find){
HTMLElement.prototype.contains = function(B){
return this.compareDocumentPosition(B) - 19 > 0
}
}

<!doctype html> <title>dom contains 方法 by 司徒正美</title> <meta charset="utf-8"/> <meta name="keywords" content="dom contains方法 by 司徒正美" /> <meta name="description" content="dom contains方法 by 司徒正美" /> <script type="text/javascript"> if(!!window.find){ HTMLElement.prototype.contains = function(B){ return this.compareDocumentPosition(B) - 19 > 0 } } window.onload = function(){ var A = document.getElementById('parent'), B = document.getElementById('child'); alert(A.contains(B)); alert(B.contains(A)); } </script> <h2 >contains方法</h2> <div id="parent"> <p> <strong id="child" >contains方法</strong> </p> </div>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

複製代碼 代碼如下:

//2011.9.24 by 司徒正美
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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.