HTML DOM ထည့်သွင်းခြင်းBefore() နည်းလမ်း
ဥပမာ
<ul> ဒြပ်စင်တစ်ခု၏ ပထမကလေးဒြပ်စင်ရှေ့တွင် <li> ဒြပ်စင်အသစ်တစ်ခုကို ထည့်သွင်းပါ-
var newItem = document.createElement("LI"); // Create a <li> node
var textnode = document.createTextNode("Water"); // Create a text node
newItem.appendChild(textnode); // Append the text to <li>
var list = document.getElementById("myList"); // Get the <ul> element to insert a new node
list.insertBefore(newItem, list.childNodes[0]); // Insert <li> before the first child of <ul>
အောက်တွင် နောက်ထပ် "စမ်းသုံးကြည့်ပါ" ဥပမာများ။
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
insertBefore() method သည် သင်သတ်မှတ်ပေးသော ရှိပြီးသားကလေးတစ်ခုရှေ့တွင် node တစ်ခုကို ကလေးအဖြစ် ထည့်သွင်းသည်။
အကြံပြုချက်- သင်သည် စာသားဖြင့် စာရင်းအကြောင်းအရာအသစ်တစ်ခုကို ဖန်တီးလိုပါက <li> ဒြပ်စင်တွင် ထပ်ဖြည့်ထားသည့် စာသား node အဖြစ် စာသားဖန်တီးရန် မမေ့ပါနှင့်၊ ထို့နောက် စာရင်းထဲသို့ <li> ထည့်ပါ။
ရှိပြီးသားဒြပ်စင်တစ်ခုကို ထည့်သွင်း/ရွှေ့ရန် insertBefore နည်းလမ်းကိုလည်း သင်အသုံးပြုနိုင်သည် ("နောက်ထပ် ဥပမာများကို ကြည့်ပါ")။
Browser ပံ့ပိုးမှု
ဇယားရှိ နံပါတ်များသည် နည်းလမ်းကို အပြည့်အဝထောက်ခံသည့် ပထမဆုံးဘရောက်ဆာဗားရှင်းကို သတ်မှတ်ပေးသည်။
Method | |||||
---|---|---|---|---|---|
insertBefore() | Yes | Yes | Yes | Yes | Yes |
အထားအသို
node.insertBefore(newnode, existingnode)
ကန့်သတ်တန်ဖိုးများ
Parameter | Type | Description |
---|---|---|
newnode | Node object | Required. The node object you want to insert |
existingnode | Node object | Required. The child node you want to insert the new node before. If set to
null , the insertBefore method will insert the newnode at the end |
နည်းပညာဆိုင်ရာအသေးစိတ်
ပြန်ပို့တန်ဖိုး- | ထည့်သွင်းထားသော node ကိုကိုယ်စားပြုသော Node Object တစ်ခု |
---|---|
DOM ဗားရှင်း | Core Level 1 Node Object |
နောက်ထပ် ဥပမာများ
ဥပမာ
စာရင်းတစ်ခုမှ အခြားတစ်ခုသို့ <li> အစိတ်အပိုင်းတစ်ခုကို ရွှေ့ပါ-
var node = document.getElementById("myList2").lastChild;
var list = document.getElementById("myList1");
list.insertBefore(node, list.childNodes[0]);