<p id="p1">This is a paragraph</p>
<p id="p2">This is another paragraph</p>
<p id="demo">请点击按钮来比较两个段落的位置。</p>
<button onclick="myFunction()">试一下</button>
var p1=document.getElementById("p1").lastChild;
var p2=document.getElementById("p2").lastChild;
var x=document.getElementById("demo");
x.innerHTML=p1.compareDocumentPosition(p2);
1: The two nodes do not belong to the same document.<br>
2: p1 is positioned after p2.<br>