কম্পিউটার

HTML DOM তুলনা ডকুমেন্ট পজিশন() পদ্ধতি


HTML DOM compareDocumentPosition() পদ্ধতিটি যেকোন নথিতে একটি প্রদত্ত নোড অবস্থানকে অন্য নোডের সাথে তুলনা করতে ব্যবহৃত হয়। নথিতে তাদের অবস্থান বর্ণনা করার জন্য এই পদ্ধতির রিটার্ন টাইপ টাইপ পূর্ণসংখ্যা। পূর্ণসংখ্যার রিটার্ন মানগুলি নির্দিষ্ট −

হিসাবে
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.

সিনট্যাক্স

HTML DOM compareDocumentPosition() পদ্ধতির জন্য সিনট্যাক্স নিচে দেওয়া হল -

node.compareDocumentPosition(node)

এখানে, নোডটি নোড অবজেক্টের ধরণের এবং নোডটি নির্দিষ্ট করে যা আমরা বর্তমান নোডের সাথে তুলনা করতে চাই।

উদাহরণ

আসুন আমরা 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>

আউটপুট

এটি নিম্নলিখিত আউটপুট −

তৈরি করবে

HTML DOM তুলনা ডকুমেন্ট পজিশন() পদ্ধতি

পজিশন বোতামে ক্লিক করলে -

HTML DOM তুলনা ডকুমেন্ট পজিশন() পদ্ধতি

উপরের উদাহরণে -

আমরা প্রথমে দুটি

তৈরি করেছি

আইডি “para1” এবং “para2” সহ উপাদান।

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

তারপরে আমরা একটি বোতাম POSTION তৈরি করেছি যা ব্যবহারকারী দ্বারা ক্লিক করার পরে docPosition() পদ্ধতিটি কার্যকর করবে -

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

ডকুমেন্ট অবজেক্টে getElementById() পদ্ধতি ব্যবহার করে docPosition() পদ্ধতিটি

উপাদান উভয়ই পায়। তারপর এটি উভয় অনুচ্ছেদের লাস্টচাইল্ড সম্পত্তি মান যথাক্রমে পরিবর্তনশীল p1 এবং p2 এ নির্ধারণ করে।

আমরা তারপর p2-এ compareDocumentPosition() পদ্ধতিটিকে p1 এর সাথে প্যারামিটার হিসাবে কল করি। এর মানে হল যে আমরা p1 এর সাথে p2 এর অবস্থান তুলনা করতে চাই। যেহেতু এখানে 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;
}

  1. HTML DOM execCommand() পদ্ধতি

  2. HTML DOM fullscreenEnabled() পদ্ধতি

  3. HTML DOM writeln() পদ্ধতি

  4. HTML DOM লিখতে() পদ্ধতি