XML DOM lastChild property
Definition and usage
The lastChild property can return the last child node of the document.
Syntax:
documentObject.lastChild
Tips and comments
Note:Internet Explorer will ignore the blank text nodes generated between nodes (such as newline symbols), while Mozilla will not do so. Therefore, in the following examples, we will use a function to check the node type of the first child node.
Element nodes' node type is 1, so if the first child node is not an element node, it will move to the next node and continue to check whether this node is an element node. The entire process will continue until the first element child node is found. In this way, we can get the correct result in Internet Explorer and Mozilla.
Tip:For mere information om forskelle mellem IE og Mozilla browserens XML DOM, besøg vores DOM browser Kapitel.
Eksempel
I alle eksempler vil vi bruge XML-filer books.xmlog de JavaScript-funktioner loadXMLDoc().
Følgende kodefragment kan vise dokumentets sidste underknudes nodenavn og nodetype:
//Check om den sidste node er en element node
function get_lastchild(n)
{
var x=n.lastChild
;
while (x.nodeType!=1)
{
x=x.previousSibling;
}
return x;
}
xmlDoc=loadXMLDoc("/example/xdom/books.xml");
var x=get_lastchild(xmlDoc);
document.write("Nodename: " + x.nodeName);
document.write(" (nodetype: " + x.nodeType + ")");
Output:
Nodename: bookstore (nodetype: 1)