外观
Today I have seen a small code snippet as below:
function rolloverInit() {
for (var i = 0; i < document.images.length; i++) {
if (document.images[i].parentNode.tagName == "A") {
setupRollover(document.images[i]);
}
}
}Then I searched for introduction about the term parentNode, and found some other related terms childNodes, parentElement, and children. Here are their usages.
parentNode
parentElement
childNodes:
children
parentNode can be used one by one. And if you want to fetch the name of the corresponding node, you can employ nodeName. Here is an example:
alert(document.getElementById("child").parentNode.parentNode.nodeName);Note that error may be returned if you place the above code snippet between the opening <head> tag and the closing </head>. This is because that all JavaScript-supported browsers will run these JavaScript code before the DOM is entirely interpreted.
If you are using Firefox, the error returned is like: document.getElementById("child") has no properties, while the error returned by IE is like: Object Required.