CHAPTER 8 SCRIPTING IN THE BROWSER 237 (Adult web hosting)
CHAPTER 8 SCRIPTING IN THE BROWSER 237 NamedNodeMap is most commonly associated with the collection of attributes within a node. A NamedNodeMap is also returned for collections of entities and notations. You can t use NamedNodeMap with the childNodescollection. NamedNodeMap has the same members as NodeList. In addition, it has the following members: getNamedItem() removeNamedItem() setNamedItem() getNamedItem(name) The getNamedItem() method retrieves a node by name using the name string parameter: oDocument.documentElement.lastChild.attributes.getNamedItem(”id”); removeNamedItem(name) This method uses the name argument to determine which node to remove. The method returns the removed node: oDocument.documentElement.lastChild.attributes.removeNamedItem(”id”); setNamedItem(newNode) The setNamedItem()method takes a Node as a parameter and adds it to the end of the NamedNodeMap: var idAttr = oDocument.documentElement.firstChild.attributes.removeNamedItem(”id”); oDocument.documentElement.lastChild.attributes.setNamedItem(”id”); The preceding code removes the id attribute from the first child and adds it to the attributes collection of the last child element. You must ensure that the node you re inserting is of the correct type. Caution NodeList and NamedNodeMap are live objects. This means that changes made to the list are reflected immediately. Therefore, you should be very careful when making changes to the list while inside a loop iterating through that list. For example, if your loop has an exit condition that relies on reaching the end of the list, adding new nodes will increase the length of the list. You ll never exit the loop because you ll never get to the end of the list. The length updates continually as the NodeList grows.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.