tailieunhanh - Học JavaScript qua ví dụ part 74

Tham khảo tài liệu 'học javascript qua ví dụ part 74', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Modifying the DOM Appending Copying and Removing Nodes 653 EXPLANATION continued 4 A reference to a new div is returned. 5 The cloned table is appended to the div just below the original table see Figure . 6 This is the div container for the cloned table. Figure Cloning a table with a unique id. Removing a Node If you want to dynamically remove a post from a blog or an ingredient from an online recipe it can be done with the DOM. To delete an element or node from the DOM tree we use the removeChildO method. This method must be called from the parent node because you will be removing a child. It takes a reference to the child it will remove as its only argument. The following examples demonstrate how to dynamically remove nodes with JavaScript and the DOM. The first example Example removes a div and its contents the second example Example removes paragraphs. From the Library of 654 Chapter 15 The W3C DOM and JavaScript FORMAT removeChild referenceToChild EXAMPLE div2 EXAMPLE html head title Removing a Child with the DOM title script type text javascript 1 function removeDiv 2 var divMid div 1 3 divMid Remove the middle div alert div .length divs left prints 2 script head body onload removeDiv 4 div this is div1 div 5 div this is div2 div 6 div this body is div3 div html EXPLANATION 1 The function called removeDiv will remove a div element from the DOM tree. 2 The getElementsByTagName method returns a reference to the second div tag in the document. The 1 index is applied to the div reference returned by getEle-mentsByTagName and retrieves the second div. 0 would be the first div in the list of div tags. 3 Using the reference to the second div we go to the parent div and remove its child the second div see Figure . 4 Three divs are defined in this document. From the Library of .

TỪ KHÓA LIÊN QUAN