tailieunhanh - JavaScript Bible, Gold Edition part 25
JavaScript Bible, Gold Edition part 25. This book will bring programmers and non-technical professionals, including casual programmers and scripters, painlessly up to speed on all aspects of mastering JavaScript. Key topics include programming fundamentals, JavaScript language elements and how to use them effectively, and how to easily and efficiently add powerful new functionality to HTML documents and Java applets. | 88 Part III Document Objects Reference At last the new element is part of the document containment hierarchy. You can now reference it just like any other element in the document. Replacing node content The addition of the paragraph shown in the last section requires a change to a portion of the text in the original paragraph the first paragraph is no longer the one and only paragraph on the page . As mentioned earlier you can perform text changes either via the replaceChild method or by assigning new text to a text node s nodeValue property. Let s see how each approach works to change the text of the first paragraph s EM element from one and only to first. To use replaceChild a script must first generate a valid text node with the new text var newText first Because strings are dumb in other words they don t know about words and spaces the new text node includes a space to accommodate the existing space layout of the original text. The next step is to use the replaceChild method. But recall that the point of view for this method is the parent of the child being replaced. The child here is the text node inside the EM element so you must invoke the replaceChild method on the EM element. Also the replaceChild method requires two parameters the first is the new node the second is a reference to the node to be replaced. Because the script statements get pretty long with the getElementById method an intermediate step grabs a reference to the text node inside the EM element var oldChild emphasis1 .childNodes 0 Now the script is ready to invoke the replaceChild method on the EM element swapping the old text node with the new emphasis1 .replaceChild newText oldChild If you want to capture the old node before it disappears entirely be aware that the replaceChild method returns a reference to the replaced node which is only in memory at this point and not part of the document node hierarchy . You can .
đang nạp các trang xem trước