Welcome to DOM World!
if (node.nodeType != null) {
temp += "nodeType:" + node.nodeType + "\n";
}
else {
temp += "nodeType:null!\n";
}
if (node.nodeValue != null) {
temp += "nodeValue:" + node.nodeValue + "\n";
}
else {
temp += "nodeValue:null!\n";
}
return temp;
}
var currentElement = document.getElementById('p1');
var msg = nodeStatus(currentElement);
//alert(msg);
var firstChild = currentElement.firstChild;
msg += nodeStatus(firstChild);
//父亲的下一个儿子->弟弟
var youngerBrother = firstChild.nextSibling;
msg += nodeStatus(youngerBrother);
//2弟的儿子=p父亲的大孙子
var grandSon = youngerBrother.firstChild;
msg += nodeStatus(grandSon);
//孙子的父亲
var grandSonParent = grandSon.parentNode;
msg += nodeStatus(grandSonParent);
//孙子的父亲的兄长
var grandSonParentElderBrother = grandSonParent.previousSibling
msg += nodeStatus(grandSonParentElderBrother);
//大哥的父亲
var parent = grandSonParentElderBrother.parentNode;
msg += nodeStatus(parent);
//父亲的小儿子
var lastChild = parent.lastChild;
msg += nodeStatus(lastChild);
//父亲的所有儿子
var allChildInfo = "";
var allChild = parent.childNodes;
for (var i = 0; i allChildInfo += nodeStatus(allChild[i]);
}
//alert(msg);
id="pSample">
This is the first Sample!
The above is the detailed content of Example code for the Document Object Model. For more information, please follow other related articles on the PHP Chinese website!