, , ID. getElementById(). oList :
var oList = document.getElementById("components")
BODY:
<!-- pr28 -->
<p id="myP">Hello, World!</p>
<script type="text/javascript">
alert(document.getElementById("myP").innerHTML);
// Hello World!
var str = "";
str += document.getElementById("myP").nodeName + '\n';
str += document.getElementById("myP").nodeValue + '\n';
str += document.getElementById("myP").nodeType + '\n';
alert(str);// P null 1
</script>
nodevalue, null, <p> , Hello, World!. , :
document.getElementById("myP").childNodes[0].nodeValue;
, , . , - parentNode, . () oList. DIV :
var oParent = oList.parentNode
, , childNodes . (- attributes) . ,
var oItem1 = oList.childNodes[1]
oItem1 <LI>CSS</LI> . DOM childNodes[1] oList. (childNodes[0]) : firstChild lastChild. . previousSibling nextSibling. - ( null, ). , childNodes[1] nextSibling childNodes[0] previousSibling childNodes[2]. , childNodes[1] :
oList.firstChild.nextSiblingoList
.childNodes[2].previousSibling
, - . ,
oList.childNodes[1].firstChild
"CSS" <LI>CSS</LI> .
oNode.
|
|
, (parentNode, firstChild, lastChild, nextSibling previousSibling), , . , , .
DOM, :
<!-- 29: DOM-->
<table>
<tr id="firstRow">
<td id="firstCell"></td>
<td id="currentNode" width="10">
<span id="spanNode">
text</span>
<p id="pNode">text</p>
</td>
<td id="lastCell"></td>
</tr>
</table>
<script type="text/javascript">
alert(document.getElementById("currentNode").parentNode.id);
// firstRow
alert(document.getElementById(currentNode").childNodes[0].id);
// spanNode
alert(document.getElementById("currentNode").firstChild.id);
// spanNode
alert(document.getElementById("currentNode").lastChild.id);
// pNode
alert(document.getElementById("currentNode").previousSibling.id);
// firstCell
alert(document.getElementById("currentNode").nextSibling.id);
// lastCell
alert(document.getElementById("currentNode").attributes["width"].
value);
// 10
alert(document.getElementById("currentNode").ownerDocument.
nodeName);
// #document
</script>
getElementById(), , . getElementsByName() name, getElementsByTagName() () . document.
DOM , :
createElement(tagName) () , ;
createTextNode(string) , .
createAttribute(name) , ;
createComment(string) HTML- <!--string -->, ;
createDocumentFragment() ;
document.
. , : <LI>XML</LI>. DOM : -<LI> "XML". , createElement() createTextNode() document.
var oItem = document.createElement("LI")
. <LI> , . , insertBefore() appendChild().
createTextNode() . nodeValue . :
|
|
var oText = document.createTextNode("XML")
C "XML". . , c , appendChild(), replaceNode() insertBefore().
, : -<LI> "XML". .
:
insertBefore(newChild, referenceChild);
appendChild(newChild).
newChild . appendChild() , . , :
oItem.appendChild(oText)
oText oItem. , , . oItem.firstChild. ( )
<LI>XML</LI>
. , , , appendChild():
oList.appendChild(oItem)
oList, oItem, , . :
<UL ID="components">
<LI>HTML</LI>
<LI>CSS</LI>
<LI>Javascript</LI>
<LI>XML</LI>
</UL>
, , , <LI>HTML</LI>. insertBefore(). insertBefore() , referenceChild. appendChild(), insertBefore() , childNodes . , , . ,
var oBrother = oList.firstChild.nextSiblingoList.insertBefore(oItem, oBrother)
childNodes oList oItem childNodes[0]. insertBefore() , , - , . . , . , , . insertBefore() .
:
<UL ID="components">
<LI>HTML</LI>
<LI>XML</LI>
<LI>CSS</LI>
<LI>Javascript</LI>
</UL>
:
<!-- pr30: -->
<html>
<head>
<title>A Simple Page</title>
<script type="text/javascript">
function modify() {
var newElem = document.createElement("p");
newElem.id = "newP";
var newText = document.createTextNode("This is the second paragraph.");
newElem.appendChild(newText);
document.body.appendChild(newElem);
document.getElementById("emphasis1").childNodes[0].nodeValue = "first ";
}
</script>
</head>
<body>
<button onClick="modify()">Add/Replace Text</button>
<p id="paragraph1">This is the <em id="emphasis1">one and only</em> paragraph on the
|
|
page.</p>
</body>
</html>
.
: cloneNode()
, , false. false, , .
, cloneNode(true). , :
var oClone = oList.cloneNode(true)
, oList, . .