up_divs = document.getElementsByName("up"); // retourne une liste de nodes
elements = document.getElementsByTagName(h1); //retourne une liste des h1
// pour <p id="p1" class="reggy" align="right">texte</p>
premierParagraphe = document.getElementById("p1");
var listeimg = document.images;
for(var i = 0; i < listeimg.length; i++) {
if(listeimg[i] == "xxx.gif") {
alert("Attention aux noms d’images"!)
}
}
var node_list = "";
function do_document () {
mark_tags(document.body);
alert ("This text has the following elements: " + node_list);
}
function mark_tags(node) {
// Check if n is an Element Node
if (node.nodeType == 1 /*Node.ELEMENT_NODE*/) {
// Append the node name to the list
node_list += node.nodeName + " ";
// Let's see if there are children
if (node.hasChildNodes()) {
// Now get all children of n
var children = node.childNodes;
// Loop through the children
for(var i=0; i < children.length; i++) {
mark_tags(children[i]); // Recurse on each one
} } } }
</script>
</head><body>
<h1>Tree walking</h1>
<input type=button onClick="do_document();" value="Show body node names">
function change_me() {
var doc = window.document;
var preface = doc.getElementById("preface");
var titre = doc.getElementById("preface_title");
// on cree un élément div
var new_div_node = doc.createElement("div") ;
// on cree un nouveau TextNode
var contenu = doc.createTextNode ("Auteur: DKS et collaborateurs");
// on le met dans le div
new_div_node.appendChild(contenu);
// et on insere ce div devant l'élément qui a l'ID "preface"
preface.insertBefore(new_div_node, titre) ;
}
.....
<div id="preface" >
<h2 id="preface_title ">La préface</h2>
blabla (qui a écrit cela ?)
</div>
<p> <button onclick="change_me() ;">Transformez-moi !</button> <p>