日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

判断节点包含

發布時間:2025/3/17 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 判断节点包含 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 元素包含

contains基本兼容
用法node.contains( otherNode )
compareDocumentPosition感覺比較奇怪,據說是這樣

The Node.compareDocumentPosition() method compares the position of the
current node against another node in any other document.
返回值分為6個數字
jquery的selector-native有個原生方法contains這樣寫的

contains: function( a, b ) {var adown = a.nodeType === 9 ? a.documentElement : a, bup = b && b.parentNode;return a === bup || !!( bup && bup.nodeType === 1 && adown.contains(bup) );}

比較容易理解


update 2015-01-27

開發鳳凰焦點項目的時候,有用到判斷元素包含關系的contains,當時第一反應是想到jquery取子元素的方式.(之前看過實現方式的,忘了2333...)當然不是用的contains,因為相比取子元素更適合關系判斷.那么jq是怎么取的呢?

  • node.nextSibling
  • node.previousSibling
    沒錯,就是這倆屬性,traversing.js腳本可見
jQuery.extend({//directiondir: function( elem, dir, until ) {var matched = [],truncate = until !== undefined;while ( (elem = elem[ dir ]) && elem.nodeType !== 9 ) {if ( elem.nodeType === 1 ) { //nodeType:1 --> elementif ( truncate && jQuery( elem ).is( until ) ) {break;}matched.push( elem );}}return matched;},sibling: function( n, elem ) {var matched = [];for ( ; n; n = n.nextSibling ) {if ( n.nodeType === 1 && n !== elem ) {matched.push( n );}}return matched;} }); siblings: function( elem ) {return jQuery.sibling( ( elem.parentNode || {} ).firstChild, elem ); }, children: function( elem ) {return jQuery.sibling( elem.firstChild ); }

轉載于:https://www.cnblogs.com/kite-Runner/p/4222920.html

總結

以上是生活随笔為你收集整理的判断节点包含的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。