ล่าสุดการพัฒนาเว็บบทเรียน
×

JavaScript การอ้างอิง

ภาพรวม

JavaScript

JS เชือก JS จำนวน JS ผู้ประกอบการ JS งบ JS คณิตศาสตร์ JS วันที่ JS แถว JS บูลีน JS นิพจน์ทั่วไป JS สถานการณ์โดยรวม JS การแปลง

เบราว์เซอร์ BOM

Window Navigator Screen History Location

HTML DOM

DOM เอกสาร DOM องค์ประกอบ DOM แอตทริบิวต์ DOM เหตุการณ์ DOM รูปแบบ

HTML วัตถุ

<a> <abbr> <address> <area> <article> <aside> <audio> <b> <base> <bdo> <blockquote> <body> <br> <button> <canvas> <caption> <cite> <code> <col> <colgroup> <datalist> <dd> <del> <details> <dfn> <dialog> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <footer> <form> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <ins> <input> button <input> checkbox <input> color <input> date <input> datetime <input> datetime-local <input> email <input> file <input> hidden <input> image <input> month <input> number <input> password <input> radio <input> range <input> reset <input> search <input> submit <input> text <input> time <input> url <input> week <kbd> <keygen> <label> <legend> <li> <link> <map> <mark> <menu> <menuitem> <meta> <meter> <nav> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <s> <samp> <script> <section> <select> <small> <source> <span> <strong> <style> <sub> <summary> <sup> <table> <td> <th> <tr> <textarea> <time> <title> <track> <u> <ul> <var> <video>

วัตถุอื่น ๆ

CSSStyleDeclaration


 

HTML DOM appendChild() Method

<ธาตุวัตถุ

ตัวอย่าง

ท้ายรายการในรายการ:

var node = document.createElement("LI");                 // Create a <li> node
var textnode = document.createTextNode("Water");         // Create a text node
node.appendChild(textnode);                              // Append the text to <li>
document.getElementById("myList").appendChild(node);     // Append <li> to <ul> with id="myList"

ก่อนที่จะผนวก:

  • Coffee
  • Tea

หลังจากการผนวก:

  • Coffee
  • Tea
  • Water
ลองตัวเอง»

เพิ่มเติม "Try it Yourself" ตัวอย่างด้านล่าง


ความหมายและการใช้งาน

appendChild() วิธีการผนวกโหนดเป็นเด็กสุดท้ายของโหนด

เคล็ดลับ: หากคุณต้องการสร้างย่อหน้าใหม่มีข้อความอย่าลืมที่จะสร้างข้อความเป็นโหนดข้อความที่คุณผนวกกับวรรคแล้วผนวกวรรคเอกสาร

นอกจากนี้คุณยังสามารถใช้วิธีนี้เพื่อย้ายองค์ประกอบจากองค์ประกอบหนึ่งไปยังอีก (See "More Examples")

เคล็ดลับ: ใช้ insertBefore() วิธีการแทรกโหนดเด็กใหม่ก่อนที่ระบุไว้ที่มีอยู่โหนดเด็ก


สนับสนุนเบราว์เซอร์

ตัวเลขในตารางระบุราว์เซอร์รุ่นแรกที่สนับสนุนอย่างเต็มที่วิธีการ

วิธี
appendChild() ใช่ ใช่ ใช่ ใช่ ใช่

วากยสัมพันธ์

node .appendChild( node )

ค่าพารามิเตอร์

พารามิเตอร์ ชนิด ลักษณะ
node Node object จำเป็นต้องใช้ โหนดวัตถุที่คุณต้องการที่จะผนวก

รายละเอียดทางเทคนิค

กลับค่า: วัตถุโหนดคิดเป็นโหนดท้าย
DOM รุ่น ระดับแกนวัตถุ 1 โหนด

ตัวอย่าง

ตัวอย่างอื่น ๆ

ตัวอย่าง

ย้ายรายการจากรายการหนึ่งไปยังอีก:

var node = document.getElementById("myList2").lastChild;
document.getElementById("myList1").appendChild(node);

ก่อนที่จะผนวก:

  • Coffee
  • Tea
  • Water
  • Milk

หลังจากการผนวก:

  • Coffee
  • Tea
  • Milk
  • Water
ลองตัวเอง»

ตัวอย่าง

สร้าง <p> องค์ประกอบและผนวกไปยัง <div> องค์ประกอบ:

var para = document.createElement("P");                       // Create a <p> node
var t = document.createTextNode("This is a paragraph.");      // Create a text node
para.appendChild(t);                                          // Append the text to <p>
document.getElementById("myDIV").appendChild(para);           // Append <p> to <div> with id="myDIV"
ลองตัวเอง»

ตัวอย่าง

สร้าง <p> องค์ประกอบที่มีข้อความบางส่วนและผนวกไปยังจุดสิ้นสุดของร่างกายเอกสาร:

var x = document.createElement("P");                        // Create a <p> node
var t = document.createTextNode("This is a paragraph.");    // Create a text node
x.appendChild(t);                                           // Append the text to <p>
document.body.appendChild(x);                               // Append <p> to <body>
ลองตัวเอง»

หน้าเว็บที่เกี่ยวข้อง

HTML DOM อ้างอิง: href="met_node_haschildnodes.html"> element . hasChildNodes() Method href="met_node_haschildnodes.html"> element . hasChildNodes() Method

HTML DOM อ้างอิง: href="met_node_insertbefore.html"> element . insertBefore() Method href="met_node_insertbefore.html"> element . insertBefore() Method

HTML DOM อ้างอิง: href="met_node_removechild.html"> element . removeChild() Method href="met_node_removechild.html"> element . removeChild() Method

HTML DOM อ้างอิง: href="met_node_replacechild.html"> element . replaceChild() Method href="met_node_replacechild.html"> element . replaceChild() Method

HTML DOM อ้างอิง: href="met_document_createelement.html">document. createElement() Method href="met_document_createelement.html">document. createElement() Method

HTML DOM อ้างอิง: href="met_document_createtextnode.html">document. createTextNode() Method href="met_document_createtextnode.html">document. createTextNode() Method

HTML DOM อ้างอิง: href="met_document_adoptnode.html">document. adoptNode() Method href="met_document_adoptnode.html">document. adoptNode() Method

HTML DOM อ้างอิง: href="met_document_importnode.html">document. importNode() Method href="met_document_importnode.html">document. importNode() Method


<ธาตุวัตถุ