2016-09-26 1 views
-2
<!-- I set the tags here--> 
<!DOCTYPE html> 
<html> 
<head> 
    <!-- I set the coding language--> 
    <script type="text/javascript"> 
    <!-- 
    <!-- The image variables can be seen here--> 
    var TrafficLights = new array() 
    image[0] = "traffic-light-red.jpg"; 
    image[1] = "traffic-light-red-amber.jpg"; 
    image[2] = "traffic-light-green.jpg"; 
    image[3] = "traffic-light-amber.jpg"; 

    <!--they are then put into an array--> 
    var TrafficLightImages = [image1, image2, image3, image4] 

    <!-- I close the script and head tags--> 
    </script> 
</head> 
<!-- I start a body tag--> 

<body> 
    <!-- Create a button with the button class= "myButton" and name it to say 
     "Change the lights!"--> 
    <!-- It will also change the image of the traffic light--> 
    <button class="myButton" onclick="slideit()">Change the lights!</button> 

    <!-- I call the URL of the image and I set its width and length to 120 and 270 
<img src="traffic-light-red.jpg" name="slide" width="152" height="320"> 

<!-- I set the coding language--> 
    <script type="text/javascript"> 
    <!-- 
    <!-- I create another variable that makes step=1--> 
    var step = 1; 
    var i = 0 
     <!--Previously this was a do/while loop but I changed as I learnt it was the 
      source of my previous problem--> 
     <!-- I create a function names slide--> 

    function UpdateImage() { 
     for (i = 0; i < TrafficLights.length; i++) { <!-- Change this --> 

      document.write("<li><img src='" + TrafficLights[i] + "'width=" 
      150 " height=" 
      110 "/>span>" + TrafficLights[i] + "</span><li>"); 


      //--> 
    </script> 
</body> 
</html> 

Die obigen Zeilen sind der Code. Wie dreht man sich durch die verschiedenen Bilder von Ampeln eines Arrays, zeigt sie an und ändert nur das Bild, wenn man den Knopf drückt.So zeigen Sie die Bilder vom Array an (vom Benutzer gesteuerte Ampeln)

Wenn möglich, zeigen Sie bitte ein voll funktionsfähiges Skript.

+1

Auf den ersten Bild1 existiert nicht, Bild [1] tut –

+1

Off Thema aber erwähnenswert. Ihre Kommentare sind überreich, sie machen Ihren Code schwerer lesbar. – Mihailo

+1

wo ist slide() Methode ?? – atul

Antwort

0

document.write() fügt am Ende des Dokuments nur HTML hinzu.

Um ein vorhandenes Tag zu ändern, benötigen Sie einen Event-Handler, der das Attribut 'src' des Image-DOM-Knotens ändert.

Verwandte Themen