Bonjour,
j'utilise cloneNode pour "dupliquer" un conteneur dont l'ID est incrémenté par un compteur. Cela fonctionne correctement; "conteneur" devient "conteneur1". La div conteneur "contient" 4 objets alpha, beta, gama, delta.
Est-il possible que les ID de ces objets soit également incrémentés pendant le clonage ie alpha1, beta1, gama1, delta1 ?
Merci
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 <html> <head> <meta charset="UTF-8"> <link href="master.css" rel="stylesheet" /> </head> <body> <script> var _counter = 0; document.getElementById("ligne").value ="1"; function Add() { if(document.getElementById("ligne").value == "1"){ _counter++; var oClone = document.getElementById("conteneur").cloneNode(true); oClone.id += (_counter); document.getElementById("conteneur").appendChild(oClone); var domElement = document.getElementById('alpha'); domElement.id.value = domElement.id.value + '1'; domElement.style.position = 'absolute'; domElement.style.top = 200; domElement.style.left = 10; domElement.style.backgroundColor = document.getElementById('alpha').style.backgroundColor; var domElement = document.getElementById('beta'); domElement.style.position = 'absolute'; domElement.style.top = 200; domElement.style.left = 30; domElement.style.backgroundColor = document.getElementById('beta').style.backgroundColor; var domElement = document.getElementById('gama'); domElement.style.position = 'absolute'; domElement.style.top = 220; domElement.style.left = 10; domElement.style.backgroundColor = document.getElementById('gama').style.backgroundColor; var domElement = document.getElementById('delta'); domElement.style.position = 'absolute'; domElement.style.top = 220; domElement.style.left = 30; domElement.style.backgroundColor = document.getElementById('delta').style.backgroundColor; document.getElementById("ligne").value ="2"; } } </script> <input type="button" onClick=" document.getElementById('alpha').style.backgroundColor = 'red'; " value="red "> <input type="button" onClick=" document.getElementById('beta').style.backgroundColor = 'blue'; " value="blue "> <input type="button" onClick="Add()" value="Click! "> Ligne: <input id="ligne" type="text" value = "1" > <div id='conteneur' style="position:relative"> <P class="verif" id='alpha' style="position:absolute; left:10px; top:50px;" ></P> <P class="verif" id='beta' style="position:absolute; left:30px; top:50px;" ></P> <P class="verif" id='gama' style="position:absolute; left:10px; top:70px;" ></P> <P class="verif" id='delta' style="position:absolute; left:30px; top:70px;" ></P> </div> </body> </html>
Partager