|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Dec 2001
Città: Fondi - LT -
Messaggi: 2989
|
[JavaScript - DOM] Domanda veloce sul metodo setAttribute
Ciao a tutti.
Vi riporto un esempio del codice che mi sta dando problemi: Codice:
var x = 10; var y = 10; var z = 20; var k = 20; var body = document.getElementById("body"); var mappa = document.createElement("map"); mappa.setAttribute("name", "map1"); var zona = document.createElement("area"); zona.setAttribute("shape", "rect"); zona.setAttribute("coords", "x,y,z,k"); zona.setAttribute("nohref", "true"); zona.setAttribute("onmouseover", "document.writeln('ciao')"); mappa.appendChild(zona); body.appendChild(mappa); Il problema è che se passo delle variabili alla setAttribute non funziona, mentre se gli do direttamente i valori numerici non ho problemi. Avete qualche consiglio da darmi per risolvere il problema?
__________________
Asus Prime X670E-PRO WIFI|AMD Ryzen 7 7800X3D|G.Skill Trident Z Neo 64GB DDR5 6000MHz CL30|nVidia GeForce RTX 4090 Founders Edition|Samsung 980 Pro 1TB|Samsung 980 Pro 2TB|Creative Sound BlasterX AE5-Plus| BeQuiet! Dark Power 13 850W|Noctua NH-D15S| BeQuiet! Silent Base 801
|
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2008
Messaggi: 411
|
Hai provato a controllare cosa ti dà in output se usi le variabili?
Intendo ad esempio utilizzando Firebug per controllare il DOM dinamicamente. |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Dec 2001
Città: Fondi - LT -
Messaggi: 2989
|
In corrispondenza dell'attributo coords mi da la stringa "x, y, z, k".
Nella specifica del DOM del metodo setAttribute (Link) se non ho capito male c'è scritto che si possono passare solo stringhe. Se si vuole passare qualcosa di diverso bisogna prepararlo a parte creando un nodo Attr e poi usare il metodo setAttributeNode per passare l'attributo creato. Solo che non ci ho capito molto. ![]()
__________________
Asus Prime X670E-PRO WIFI|AMD Ryzen 7 7800X3D|G.Skill Trident Z Neo 64GB DDR5 6000MHz CL30|nVidia GeForce RTX 4090 Founders Edition|Samsung 980 Pro 1TB|Samsung 980 Pro 2TB|Creative Sound BlasterX AE5-Plus| BeQuiet! Dark Power 13 850W|Noctua NH-D15S| BeQuiet! Silent Base 801
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Dec 2001
Città: Fondi - LT -
Messaggi: 2989
|
Ho risolto.
In pratica la risposta al problema me l'ero già data da solo dicendo che la setAttribute vuole una stringa... La soluzione è questa: Codice:
var x = 10; var y = 10; var z = 20; var k = 20; var coordinate = new String(x + "," + y + "," + z + "," + k); var body = document.getElementById("body"); var mappa = document.createElement("map"); mappa.setAttribute("name", "map1"); var zona = document.createElement("area"); zona.setAttribute("shape", "rect"); zona.setAttribute("coords", coordinate); zona.setAttribute("nohref", "true"); zona.setAttribute("onmouseover", "document.writeln('ciao')"); mappa.appendChild(zona); body.appendChild(mappa); ![]() BYEZZZZZZZZZZ!!!!!!!!!!
__________________
Asus Prime X670E-PRO WIFI|AMD Ryzen 7 7800X3D|G.Skill Trident Z Neo 64GB DDR5 6000MHz CL30|nVidia GeForce RTX 4090 Founders Edition|Samsung 980 Pro 1TB|Samsung 980 Pro 2TB|Creative Sound BlasterX AE5-Plus| BeQuiet! Dark Power 13 850W|Noctua NH-D15S| BeQuiet! Silent Base 801
|
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Nov 2008
Messaggi: 411
|
Perfetto
![]() |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 18:26.