Speaking directly from the method:
1, delete the node. RemoveChild ():
<body>
<div id= "Cssrain" >
<div id= "a" >a </div>
<div id= "B" >b </div>
<div id= "C" >c </div>
</div>
</body>
<script>
var msg = document.getElementById ("Cssrain");
var B = document.getElementById ("B");
Msg.removechild (b);
</script>
If you do not know what the parent node of the node you want to delete is? You can use the ParentNode property.
Like what:
<body>
<div id= "Cssrain" >
<div id= "a" >a </div>
<div id= "B" >b </div>
<div id= "C" >c </div>
</div>
</body>
<script>
var B = document.getElementById ("B");
var c = B.parentnode;
C.removechild (b);
</script>
2, replace the node. Repalcechild ()
Element.repalcechild (NewNode, OldNode); The new node is the guest, must first serve him slightly. The OldNode must be a child node of the element.
Example:
<body>
<div id= "Cssrain" >
<div id= "a" >a </div>
<div id= "B" >b </div>
<div id= "C" >c </div>
</div>
</body>
<script>
var cssrain = document.getElementById ("Cssrain");
var msg = document.getElementById ("B");
var para = document.createelement ("P");
Cssrain.replacechild (Para, msg);
</script>
3, find the node
Finding nodes is relatively straightforward relative to the above method.
Because a lot of people have used it. (Remember the first thing I know about JS is getElementById ();)
getElementById ();
Returns an object that has attributes such as NodeName, NodeType, ParentNode, ChildNodes, and so on.
getElementsByTagName () finds all the elements of the label name.
Returns a collection that can be used to iterate through each object with attributes such as NodeName, NodeType, ParentNode, ChildNodes, and so on.
Example:
var PS = document.getelementsbytagname ("P");
for (var i=0; i< ps.length; i++) {
Ps[i].setattribute ("title", "Hello");
You can also use: Ps.item (i) setattribute ("title", "Hello");
}
Current 1/2 page
12 Next read the full text