Bonjour,
Voici ce que j'aimerais faire :
J'ai un UL que je popule dynamiquement avec des LI selon des données (ici l'array artids).
Quand je clique sur un LI, j'aimerais avoir le "numéro" du LI (1 pour le premier, etc).
Mais pour une raison qui m'échappe (je suspecte le bug), le code suivant ne marche pas !!
Pourtant je met bien la valeur dans une variable (xCopy) qui n'existe qu'à chaque itération de la boucle.
Code : 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 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> <title>IP</title> </head> <body bgcolor="#ffffff"> <ul id="ull"> </ul> <script language="javascript" type="text/javascript"> <!-- var ul = document.getElementById("ull"); var artids = [ { "firstChild" : { "data" : "a" } } , { "firstChild" : { "data" : "b" } } , { "firstChild" : { "data" : "c" } } ]; for(var x = 0; x < artids.length; x++) { var artid = artids[x].firstChild; var v = ""; do { if (artid.data) { v += artid.data; } } while (artid = artid.nextSibling); var liElem = document.createElement("li"); liElem.id = x; var xCopy = x; fMouseDown = function(evt) { // CurrentPage.suggestChange(input, liElem); // alert(evt.currentTarget.id); alert(xCopy); } liElem.onmousedown = fMouseDown; var txt = document.createTextNode(v); liElem.appendChild(txt); ul.appendChild(liElem); } </script> </body> </html>
Merci pour vos lumières.
Partager