日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

HTML DOM CompareDocumentPosition() 方法用于將給定節(jié)點(diǎn)位置與任何文檔中的任何其他節(jié)點(diǎn)進(jìn)行比較。該方法的返回類型是整數(shù)類型,用于描述它們?cè)谖臋n中的位置。整數(shù)返回值如指定 –

1: No relationship, the two nodes do not belong to the same document.
2: The first node (para1) is positioned after the second node (para2).
4: The first node (para1) is positioned before the second node (para2).
8: The first node (para1) is positioned inside the second node (para2).
16: The second node (para2) is positioned inside the first node (para1).
32: No relationship, or the two nodes are two attributes on the same element.

登錄后復(fù)制

語法

以下是 HTML DOM CompareDocumentPosition() 方法的語法 –

node.compareDocumentPosition(node)

登錄后復(fù)制

這里的節(jié)點(diǎn)是節(jié)點(diǎn)對(duì)象類型,指定我們要與當(dāng)前節(jié)點(diǎn)進(jìn)行比較的節(jié)點(diǎn)。

示例

讓我們看一個(gè)compareDocumentPosition的示例() 方法 –

<!DOCTYPE html>
<html>
<body>
<p id="para1">This is a paragraph</p>
<p id="para2">This is another paragraph</p>
<p>Click the button to compare the position of the two paragraphs.</p>
<button onclick="docPosition()">POSITION</button>
<p id="Sample"></p>
<script>
   function docPosition() {
      var p1 = document.getElementById("para1").lastChild;
      var p2 = document.getElementById("para2").lastChild;
      var x = p2.compareDocumentPosition(p1);
      document.getElementById("Sample").innerHTML = x;
   }
</script>
</body>
</html>

登錄后復(fù)制

輸出

這將產(chǎn)生以下輸出 –

單擊“位置”按鈕時(shí) –

在上面的示例中 –

我們首先創(chuàng)建了兩個(gè) id 為“para1”和“的元素

第2段”。

<p id="para1">This is a paragraph</p>
<p id="para2">This is another paragraph</p>

登錄后復(fù)制

然后我們創(chuàng)建了一個(gè)按鈕 POSTION,它將在用戶單擊時(shí)執(zhí)行 docPosition() 方法 –

<button onclick="docPosition()">POSITION</button>

登錄后復(fù)制

docPosition() 方法使用文檔對(duì)象上的 getElementById() 方法獲取 <p> 元素。然后,它將兩個(gè)段落的lastchild 屬性值分別賦給變量p1 和p2。

然后,我們以p1 作為參數(shù),調(diào)用p2 上的compareDocumentPosition() 方法。這意味著我們要比較 p2 相對(duì)于 p1 的位置。由于這里 p2 位于 p1 之后,因此返回值為 2 –

function docPosition() {
   var p1 = document.getElementById("para1").lastChild;
   var p2 = document.getElementById("para2").lastChild;
   var x = p2.compareDocumentPosition(p1);
   document.getElementById("Sample").innerHTML = x;
}

登錄后復(fù)制

以上就是HTML DOM compareDocumentPosition() 方法

比較文檔位置的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!

分享到:
標(biāo)簽:DOM html 位置 文檔 方法
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定