Thuta Learning
JavaScript
IntermediateProgrammingအခြေခံ

JS DOM Intro

JavaScriptသင်ခန်းစာ 2420 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • HTML တစ်ပိုင်းကို DOM tree (element node, text node) အဖြစ် ပုံဆွဲပြနိုင်ဖို့
  • document object ကနေ element တွေကို ရှာပြီး textContent ပြောင်းနိုင်ဖို့
  • parentNode, childNodes, children တို့နဲ့ tree ထဲမှာ လှည့်လည်နိုင်ဖို့
  • Script က element မရှိသေးခင် run မိလို့ null ရတဲ့ ပြဿနာကို defer သို့မဟုတ် DOMContentLoaded နဲ့ ဖြေရှင်းနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

HTML file က အိမ်တစ်လုံးရဲ့ ပုံကြမ်း (blueprint) နဲ့ တူပါတယ်။ Browser က အဲဒီ ပုံကြမ်းကို ဖတ်ပြီး တကယ့်အိမ်ကို ဆောက်လိုက်ပါတယ် — အဲဒီ အိမ်က DOM (Document Object Model) ပါ။ JavaScript က ပုံကြမ်းကို မပြင်ပါဘူး၊ ဆောက်ပြီးသား အိမ်ထဲ ဝင်ပြီး နံရံဆေးသုတ်တာ၊ ပရိဘောဂ ရွှေ့တာ လုပ်ပါတယ်။

DOM က tree ပုံစံ ဖြစ်ပါတယ်။ အပေါ်ဆုံးမှာ document ရှိပြီး အောက်မှာ html၊ အဲဒီအောက်မှာ head နဲ့ body၊ body အောက်မှာ page ထဲက element တွေ ဆက်ရှိပါတယ်။ Tag တစ်ခုချင်းက element node ဖြစ်ပြီး tag ထဲက စာသားတွေက text node ဖြစ်ပါတယ်။ JavaScript က အဲဒီ node တွေကို object တွေလို ကိုင်တွယ်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: element ရှိပါလျက် null ပြန်ရတာ

Beginner အများစု ပထမဆုံး ကြုံရတဲ့ DOM error က ဒါပါ။ HTML ထဲမှာ <p id="greeting"> ရှိနေတာ မျက်စိနဲ့ မြင်နေရပါတယ်၊ ဒါပေမယ့် page ဖွင့်လိုက်တာနဲ့ console မှာ "Cannot set properties of null (setting 'textContent')" ဆိုတဲ့ TypeError ပေါ်ပါတယ်။ Script ကို body အောက်ဆုံးကို ရွှေ့လိုက်တော့ ဘာကြောင့်မှန်း မသိဘဲ အလုပ်ဖြစ်သွားပါတယ်။

index.html
<!doctype html>
<html>
  <head>
    <script>
      // Runs immediately: the <body> has not been parsed yet
      const greeting = document.getElementById("greeting"); // null
      greeting.textContent = "Hello from the DOM!";         // TypeError
    </script>
  </head>
  <body>
    <p id="greeting">Loading...</p>
  </body>
</html>

ဘာကြောင့် null ဖြစ်တာလဲ

Browser က HTML ကို အပေါ်ကနေ အောက်ကို ဖတ်ရင်း DOM ကို တဖြည်းဖြည်း ဆောက်ပါတယ်။ ရိုးရိုး <script> tag ကို တွေ့တာနဲ့ parsing ကို ရပ်ပြီး script ကို ချက်ချင်း run ပါတယ်။ အဲဒီအချိန်မှာ <p> ကို မဖတ်ရသေးလို့ DOM ထဲမှာ မရှိသေးပါဘူး၊ ဒါကြောင့် getElementById က null ပြန်ပါတယ်။ Script က DOM ဆောက်ပြီးမှ run အောင် defer ကို သုံးရပါမယ်။

Core concept: DOM က HTML ကနေ ဆောက်ထားတဲ့ node tree

Browser က HTML text ကို parse လုပ်ပြီး memory ထဲမှာ object tree တစ်ခု ဆောက်ပါတယ်။ အဲဒီ tree ရဲ့ အမြစ်က document object ဖြစ်ပြီး JavaScript က DOM ကို ကိုင်တွယ်တိုင်း document ကနေ စပါတယ် — document.body၊ document.getElementById() စသဖြင့်ပါ။ Node တိုင်းမှာ parentNode (မိဘ)၊ childNodes (သားသမီး အားလုံး) နဲ့ nextSibling (ညီအစ်ကို) လို ဆက်သွယ်ချက်တွေ ရှိလို့ tree ထဲမှာ အပေါ်အောက် ဘေးတိုက် သွားလို့ ရပါတယ်။

Node အမျိုးအစားnodeTypeဥပမာ
Element1 (Node.ELEMENT_NODE)<p>, <section>, <button>
Text3 (Node.TEXT_NODE)<p> ထဲက "Hello" စာသား
Comment8 (Node.COMMENT_NODE)<!-- note -->
Document9 (Node.DOCUMENT_NODE)document ကိုယ်တိုင်

Node တိုင်းက property နဲ့ method တွေ ပါတဲ့ JavaScript object အစစ်ပါ။ Element node တစ်ခုက သူ့ရဲ့ nodeName (HTML element အတွက် "SECTION" လို စာလုံးကြီးနဲ့ အမြဲ ပြန်ပါတယ်)၊ id၊ className နဲ့ textContent ကနေ စာသားကို သိပါတယ်။ အဲဒီ property တွေကို ဖတ်တာက ဘာမှ မထိခိုက်ပါဘူး၊ assign လုပ်လိုက်တာနဲ့ user မြင်ရတာ ပြောင်းသွားပါတယ်။ Element တွေ ရှာတာ၊ ပြောင်းတာ၊ event တွေကို တုံ့ပြန်တာကို နောက် lesson တွေမှာ တစ်ခုချင်း လေ့လာပါမယ်။

DOM က HTML file နဲ့ အမြဲ တူတာ မဟုတ်ပါဘူး။ Browser က ပိတ်ဖို့ မေ့ထားတဲ့ tag တွေကို ဖြည့်ပေးပြီး JavaScript ပြောင်းလိုက်တဲ့ အပြောင်းအလဲတွေကလည်း DOM ထဲမှာပဲ ရှိပြီး file ထဲ ပြန်မရောက်ပါဘူး။ ဒါကြောင့် "View Source" က မူလ HTML ကို ပြပြီး DevTools ရဲ့ Elements panel က လက်ရှိ DOM ကို ပြပါတယ်။ Page ကို refresh လုပ်လိုက်ရင် JavaScript ပြောင်းထားတာတွေ အကုန်ပျောက်ပြီး HTML ကနေ ပြန်ဆောက်ပါတယ်။

သတိထားရမယ့်အချက်တစ်ခုက DOM ဟာ JavaScript language ရဲ့ အစိတ်အပိုင်း မဟုတ်ဘဲ browser က ပေးတဲ့ Web API တစ်ခု ဖြစ်တာပါ။ Node.js မှာ document object မရှိလို့ DOM code ကို run ရင် "document is not defined" error ရပါတယ်။ နောက်ဆုံးအနေနဲ့ DOM ကို ပြောင်းတိုင်း browser က style ပြန်တွက်တာ၊ layout ချတာ၊ paint လုပ်တာတွေ လုပ်ရနိုင်လို့ အပြောင်းအလဲ အများကြီးကို တစ်စုတည်း လုပ်တာက ပိုမြန်ပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: HTML → tree → JavaScript
  1. Browser က HTML ကို အပေါ်ကအောက် ဖတ်
  2. Tag နဲ့ စာသားကို node အဖြစ် ဆောက်
  3. JS က document ကနေ node ကို ရှာ
  4. Node ပြောင်းရင် screen ပြန်ဆွဲ

JavaScript ပြင်တာက HTML file ကို မဟုတ်ဘဲ memory ထဲက tree ကိုပါ — refresh လုပ်ရင် tree ကို HTML ကနေ ပြန်ဆောက်ပါတယ်။

အထွေထွေ ပုံစံ

javascript
const element = document.getElementById("some-id"); // element or null
element.textContent = "new text";                      // change the page

element.parentNode;        // one level up
element.children;          // child elements only
element.childNodes;        // children including text nodes

document.addEventListener("DOMContentLoaded", () => {
  // the whole HTML has been parsed: every element exists
});

DOM အလုပ်တိုင်း document ကနေ စပြီး element တစ်ခုကို ရှာ၊ အဲဒီ node ရဲ့ property တွေကို ဖတ်/ပြောင်းပါတယ်။ children က element တွေကိုပဲ ပြန်ပြီး childNodes က whitespace text node တွေပါ ပြန်ပါတယ်။ Script က element မရှိခင် run နိုင်ရင် DOMContentLoaded ကို စောင့်ပါ။

Example: profile card တစ်ခုရဲ့ DOM tree ကို ကြည့်ခြင်း

ဒီ code က လိုအပ်တဲ့ element တွေကို ကိုယ်တိုင် ဆောက်လို့ browser ရဲ့ DevTools console (blank page ဖြစ်တဲ့ about:blank မှာ) ထဲကို paste လုပ်ပြီး run လို့ရပါတယ်။ Profile card တစ်ခု ဆောက်ပြီး printTree() က tree ကို indentation နဲ့ print ထုတ်ပါတယ်၊ ပြီးရင် text node တစ်ခုကို ပြောင်းပါတယ်။

dom-tree.js
// Run in a browser: paste into the DevTools console of a blank page.
const card = document.createElement("section");
card.id = "profile";
card.innerHTML = "<h2>Aung Kyaw</h2><p>Web developer</p>";
document.body.append(card);

function printTree(node, depth = 0) {
  const indent = "  ".repeat(depth);
  if (node.nodeType === Node.TEXT_NODE) {
    console.log(`${indent}#text "${node.textContent}"`);
    return;
  }
  console.log(indent + node.nodeName);
  for (const child of node.childNodes) printTree(child, depth + 1);
}

printTree(card);
console.log(card.parentNode === document.body);
document.querySelector("#profile p").textContent = "Senior web developer";
console.log(card.lastChild.textContent);

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Create and attachdocument.body.append(card);

    createElement() က node ကို memory ထဲမှာ ဆောက်ရုံပါ။ append() နဲ့ body ထဲ ထည့်မှ document tree ရဲ့ အစိတ်အပိုင်း ဖြစ်ပြီး screen ပေါ် ပေါ်လာပါတယ်။

  2. Text nodes are nodes tooif (node.nodeType === Node.TEXT_NODE) {

    nodeType နဲ့ node အမျိုးအစားကို ခွဲပါတယ်။ "Aung Kyaw" စာသားက h2 ရဲ့ property မဟုတ်ဘဲ h2 ရဲ့ child ဖြစ်တဲ့ text node သီးသန့်ပါ။

  3. Walk the childrenfor (const child of node.childNodes) printTree(child, depth + 1);

    childNodes ထဲက child တိုင်းအတွက် printTree() ကို ပြန်ခေါ်ပြီး indentation တစ်ဆင့် တိုးပါတယ်။ ဒါက tree ကို အပေါ်ကနေ အောက်ဆုံးထိ လှည့်ပတ်တဲ့ ပုံစံပါ။

  4. Parent linkconsole.log(card.parentNode === document.body);

    append() လုပ်ပြီးနောက် card ရဲ့ parentNode က body ဖြစ်လို့ true ပါ။ Tree ထဲမှာ အပေါ်ကို ပြန်တက်တဲ့ လင့်ခ်ပါ။

  5. Change the treedocument.querySelector("#profile p").textContent = "Senior web developer";

    textContent ကို assign လုပ်လိုက်တော့ p ထဲက text node ကို အသစ်နဲ့ အစားထိုးပြီး page က ချက်ချင်း update ဖြစ်ပါတယ်။ card.lastChild က အဲဒီ p ဖြစ်လို့ စာသားအသစ်ကို ပြပါတယ်။

You should see
SECTION
  H2
    #text "Aung Kyaw"
  P
    #text "Web developer"
true
Senior web developer

Script ကို ဘယ်နေရာ၊ ဘယ်အချိန် run မလဲ

ခေတ်မီ project တွေမှာ အရိုးရှင်းဆုံး ဖြေရှင်းနည်းက <script src="app.js" defer></script> ကို head ထဲမှာ ထည့်တာပါ။ defer ပါတဲ့ script က HTML parsing နဲ့ ပြိုင်တူ download လုပ်ပြီး document တစ်ခုလုံး parse ပြီးမှ (DOMContentLoaded မတိုင်ခင်) run ပါတယ်။ type="module" script တွေက defer လိုပဲ ပုံမှန်အားဖြင့် နောက်ကျပြီးမှ run ပါတယ်။ Inline script ကိုတော့ DOMContentLoaded event ထဲ ထည့်ရပါတယ်။

index.html
<head>
  <!-- runs after parsing finishes, so #greeting exists -->
  <script src="app.js" defer></script>
</head>

DOMContentLoaded နဲ့ load event ကို မရောထွေးပါနဲ့။ DOMContentLoaded က HTML parse ပြီးလို့ defer script တွေ run ပြီးတဲ့အခါ ဖြစ်ပါတယ်။ load ကတော့ ပုံတွေ၊ stylesheet တွေ စတဲ့ resource အားလုံး download ပြီးမှ နောက်ကျပြီး ဖြစ်ပါတယ်။ DOM ကို setup လုပ်ဖို့ DOMContentLoaded က လုံလောက်ပါတယ်၊ load ကို စောင့်ရင် page ကို အကြောင်းမဲ့ နှေးစေပါတယ်။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

React၊ Vue၊ Svelte လို framework တွေ သုံးရင်တောင် နောက်ဆုံးမှာ screen ပေါ်က အရာအားလုံးက DOM node တွေပါပဲ — framework က ကိုယ့်ကိုယ်စား DOM ကို update လုပ်ပေးတာပါ။ Bug တစ်ခု ရှာတဲ့အခါ DevTools ရဲ့ Elements panel မှာ element ကို right-click နှိပ်ပြီး class၊ attribute နဲ့ text တွေ တကယ် ဘာဖြစ်နေလဲ ကြည့်တာက အမြန်ဆုံး နည်းလမ်းပါ။ Console မှာ $0 က Elements panel မှာ ရွေးထားတဲ့ element ကို ညွှန်လို့ ချက်ချင်း စမ်းကြည့်လို့ ရပါတယ်။

Feature တစ်ခု မရေးခင် profile card လို component တစ်ခုကို DOM tree အဖြစ် စာရွက်ပေါ်မှာ ပုံကြမ်းဆွဲပြီး button နှိပ်လိုက်ရင် ဘယ် text node၊ ဘယ် class၊ ဘယ် attribute တွေ ပြောင်းမလဲ မှတ်ထားတာ အသုံးဝင်ပါတယ်။ Test တွေမှာတော့ Jest သို့မဟုတ် Vitest ကို jsdom environment နဲ့ run ပြီး Node.js ထဲမှာ DOM အတု တစ်ခုကို အသုံးပြုပါတယ်၊ ဒါကြောင့် browser မဖွင့်ဘဲ DOM code ကို test လုပ်လို့ ရပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Element မရှိသေးခင် script run မိတာ

head ထဲက defer မပါတဲ့ script က body ကို မဖတ်ရသေးခင် run လို့ selector တိုင်း null ပြန်ပါတယ်။ defer ထည့်ပါ၊ type="module" သုံးပါ ဒါမှမဟုတ် DOMContentLoaded ကို စောင့်ပါ။

2. childNodes ထဲမှာ element တွေပဲ ရှိမယ် ထင်တာ

HTML ကို indentation နဲ့ ရေးထားရင် tag တွေကြားက line break နဲ့ space တွေက whitespace text node တွေ ဖြစ်ပါတယ်။ ul.childNodes[0] က li မဟုတ်ဘဲ text node ဖြစ်နိုင်ပါတယ်။ Element တွေပဲ လိုရင် children နဲ့ firstElementChild ကို သုံးပါ။

3. DOM code ကို Node.js မှာ run တာ

node app.js နဲ့ run ရင် "ReferenceError: document is not defined" ရပါတယ်၊ document က browser က ပေးတာမို့ပါ။ DOM code ကို browser ထဲမှာ run ပါ၊ test လုပ်ဖို့ဆိုရင် jsdom လို DOM simulation ကို သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • External script တွေကို head ထဲမှာ defer နဲ့ ထည့်ပါ၊ HTML download နဲ့ script download က ပြိုင်တူ ဖြစ်ပါတယ်။
  • DOM ကို ဘယ်လိုပြောင်းရမလဲ မသေချာရင် DevTools Elements panel မှာ အရင်ကြည့်ပြီး $0 နဲ့ console ထဲမှာ စမ်းပါ။
  • Element အများကြီး ထည့်ရမယ်ဆိုရင် တစ်ခုချင်း ထည့်မယ့်အစား ဆောက်ပြီးမှ တစ်ကြိမ်တည်း append လုပ်ပါ။

Mini challenge

Mini challenge: tree ထဲမှာ အပေါ်တက်ပြီး ရေတွက်ပါ

Element တစ်ခုကနေ html အထိ မိဘ chain ကို ပြတဲ့ pathTo() နဲ့ subtree ထဲက element node နဲ့ text node အရေအတွက်ကို ရေတွက်တဲ့ countNodes() ကို ရေးပါ။ Solution က လိုအပ်တဲ့ element တွေကို ကိုယ်တိုင် ဆောက်ရပါမယ်။

  • <div><article><p>Read <strong>this</strong> first</p></article></div> ကို ဆောက်ပြီး body ထဲ ထည့်ပါ
  • pathTo(strong) က "HTML > BODY > DIV > ARTICLE > P > STRONG" ပြန်ရမယ် (parentElement ကို သုံးပါ)
  • countNodes(div) က element 4 ခုနဲ့ text node 3 ခု ရေတွက်ရမယ်
  • Recursion နဲ့ childNodes ကို သုံးပါ
tree-walk.js
// TODO: create the div, set its HTML, append it to document.body

function pathTo(element) {
  // TODO: collect nodeName values while walking up parentElement
  // TODO: return them joined with " > ", starting from HTML
}

function countNodes(node) {
  // TODO: count this node (Node.ELEMENT_NODE or Node.TEXT_NODE)
  // TODO: add the counts of every child in node.childNodes
  return { elements: 0, texts: 0 };
}

// TODO: print pathTo(the strong element) and the counts for the div

ရလဒ်: Console မှာ ဒီလို ပေါ်ရပါမယ်: HTML > BODY > DIV > ARTICLE > P > STRONG Elements: 4, text nodes: 3

အရိပ်အမြွက် ကြည့်ရန်

html element ရဲ့ parentElement က null ဖြစ်လို့ while (current !== null) loop က အဲဒီမှာ ရပ်ပါတယ်။ Array ရဲ့ ရှေ့ဆုံးကို ထည့်ဖို့ unshift() ကို သုံးရင် join() လုပ်ချိန်မှာ HTML က ရှေ့ဆုံး ရောက်နေပါမယ်။

အဖြေ ကြည့်ရန်
tree-walk.js
// Builds its own elements, so it works on any blank page.
const box = document.createElement("div");
box.innerHTML = "<article><p>Read <strong>this</strong> first</p></article>";
document.body.append(box);

function pathTo(element) {
  const names = [];
  let current = element;
  while (current !== null) {
    names.unshift(current.nodeName);
    current = current.parentElement;
  }
  return names.join(" > ");
}

function countNodes(node) {
  const counts = { elements: 0, texts: 0 };
  if (node.nodeType === Node.ELEMENT_NODE) counts.elements += 1;
  if (node.nodeType === Node.TEXT_NODE) counts.texts += 1;
  for (const child of node.childNodes) {
    const inner = countNodes(child);
    counts.elements += inner.elements;
    counts.texts += inner.texts;
  }
  return counts;
}

const strong = box.querySelector("strong");
console.log(pathTo(strong));
const { elements, texts } = countNodes(box);
console.log(`Elements: ${elements}, text nodes: ${texts}`);

Quick Quiz 1 / 3

<p>Hi <b>there</b></p> မှာ p element ရဲ့ childNodes.length က ဘယ်လောက်လဲ။

Quick Quiz 2 / 3

head ထဲက defer မပါတဲ့ inline <script> က body ထဲက <div id="app"> အတွက် document.getElementById("app") ကို ခေါ်ရင် ဘာပြန်ရမလဲ။

Quick Quiz 3 / 3

console.log(document.title) ကို Node.js မှာ run ရင် ဘာဖြစ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • DOM က browser က HTML ကနေ ဆောက်တဲ့ node tree ဖြစ်ပြီး document က အမြစ်ပါ။
  • Tag တွေက element node၊ စာသားတွေက text node ဖြစ်ပြီး parentNode၊ children၊ childNodes နဲ့ tree ထဲ သွားလို့ရပါတယ်။
  • JavaScript က memory ထဲက DOM ကို ပြောင်းတာပါ၊ HTML file ကို မပြောင်းပါဘူး — refresh လုပ်ရင် ပြန်ဆောက်ပါတယ်။
  • Script က element မရှိခင် run ရင် null ရပါတယ် — defer၊ type="module" ဒါမှမဟုတ် DOMContentLoaded သုံးပါ။

MDN — Document Object Model (DOM) — MDN Web Docs

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

JS DOM Intro | Thuta Learning