एचटीएमएल डोम डालने से पहले () विधि
उदाहरण
<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>
नीचे और अधिक "इसे स्वयं आज़माएं" उदाहरण।
परिभाषा और उपयोग
इंसर्ट बिफोर () विधि एक मौजूदा बच्चे से ठीक पहले एक बच्चे के रूप में एक नोड सम्मिलित करती है, जिसे आप निर्दिष्ट करते हैं।
युक्ति: यदि आप टेक्स्ट के साथ एक नया सूची आइटम बनाना चाहते हैं, तो टेक्स्ट को टेक्स्ट नोड के रूप में बनाना याद रखें जिसे आप <li> तत्व से जोड़ते हैं, फिर सूची में <li> डालें।
आप मौजूदा तत्व को सम्मिलित/स्थानांतरित करने के लिए insertBefore विधि का भी उपयोग कर सकते हैं ("अधिक उदाहरण देखें")।
ब्राउज़र समर्थन
तालिका में संख्याएं पहले ब्राउज़र संस्करण को निर्दिष्ट करती हैं जो पूरी तरह से विधि का समर्थन करता है।
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 |
टेक्निकल डिटेल
प्रतिलाभ की मात्रा: | एक नोड ऑब्जेक्ट, सम्मिलित नोड का प्रतिनिधित्व करता है |
---|---|
डोम संस्करण | कोर स्तर 1 नोड वस्तु |
और ज्यादा उदाहरण
उदाहरण
एक <li> तत्व को एक सूची से दूसरी सूची में ले जाएं:
var node = document.getElementById("myList2").lastChild;
var list = document.getElementById("myList1");
list.insertBefore(node, list.childNodes[0]);