Bonsoir,
Dans ce programme mon but est d'alterner les images or ma fonction div.style.backgroundImage.name === "url(images/image0.png)" ne fonctionne pas !
Voici l'ensemble de mes codes :
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 <!DOCTYPE html> <html lang="fr"> <head> <link rel="stylesheet" href="style.css" /> <title>DOM6</title> <script src="scripts.js"></script> </head> <body> <button type="button" onClick="changer_couleur_div()">changer de couleur !</button> <div id="div1" onmouseover="changer_couleur_survol_div()"></div> </body> </html>
Code CSS : 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 #div1 { height:20cm; margin-left:25%; margin-right:25%; background-color:white; background-image:url(images/image0.png); background-size:30%; } body { background-color:gray; } button { width:20%; height:13%; margin-left:40%; }
Code JavaScript : 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 function changer_couleur_div() { "use strict"; var div = document.body.firstChild.nextSibling.nextSibling.nextSibling; if (div.style.background === "white") { div.style.background = "yellow"; } else { div.style.background = "white"; } } function changer_couleur_survol_div() { "use strict"; var div = document.body.firstChild.nextSibling.nextSibling.nextSibling; if (div.style.backgroundImage.name === "url(images/image0.png)") { div.style.backgroundImage = "url(images/image1.png)"; } if (div.style.backgroundImage === "url(images/image1.png)") { div.style.backgroundImage = "url(images/image2.png)"; } if (div.style.backgroundImage === "url(images/image2.png)") { div.style.backgroundImage = "url(images/image3.png)"; } if (div.style.backgroundImage === "url(images/image3.png)") { div.style.backgroundImage = "url(images/image0.png)"; } }
Mon dossier ressemble à ceci :
Merci
Partager