简体   繁体   中英

My javascript function is working partially/ not working properly

I have separated files for the HTML and JS code.. I want to make a function which allows me to change the image when I click on it with another (like an array of images(?)) the HTML-JS link is done like this:

<script src="file.js"></script>

My HTML is here:

`

<!DOCTYPE html>
<html>
 <head>
    <script src="proiect.js"></script>
 </head>
<body>
    <img src="images/image7.jpg" alt="path not found" class="dynamic-image" onclick="wrapper(1)">
    <img src="images/image10.jpg" alt="path not found" class="dynamic-image" onclick="wrapper(0)">
</body>
</html>

`

My JS code:

var wrapper = function changeImg(param) {
'use strict';

var preloads=[],c;


function preload(){

    for(c=0;c<arguments.length;c++) {
        preloads[preloads.length]=new Image();
        preloads[preloads.length-1].src=arguments[c];
    }
    c=0;

}
if (param==1){
    preload('src1');
    alert("param=1");}
    if(param==0){
        preload('src2');
        alert("param=0");
    }

    document.getElementsByClassName('dynamic-image').addEventListener('click',
        function() {
            c++;
            if(c==preloads.length) {
                c=0;
            }
            this.src=preloads[c].src;
        });
}
wrapper(param);

My problem is that when I click on the image it shows the message (param=1 / param=0), but it won't change the image and I don't understand why...

Always put your scripts at the end of your body because your script need to wait that all your ressources are loaded.

You need to do just this:

<!DOCTYPE html>
<html>
 <head>

 </head>
<body>
    <img src="images/image7.jpg" alt="path not found" class="dynamic-image" onclick="wrapper(1)">
    <img src="images/image10.jpg" alt="path not found" class="dynamic-image" onclick="wrapper(0)">
    <script src="proiect.js"></script> <!-- SCRIPT TAG AT THE END OF BODY TAG -->
</body>
</html>

The entire problem is this: when you put your script tag in the head , your function triggers before the DOM is created... so it executes before the images are even created. The best-practice is to put your scripts at the end of the body tag.

Your JavaScript file needs to be on the bottom of the page, after all <img> tags are loaded.

You can format your HTML as so:

<!doctype html>
<html>
    <body>
        <img src="images/image7.jpg" alt="path not found" class="dynamic-image" onclick="wrapper(1)">
        <img src="images/image10.jpg" alt="path not found" class="dynamic-image" onclick="wrapper(0)">
        <script type="text/javascript" src="proiect.js"></script>
    </body>
</html>

You don't need your "wrapper(param);" in your proiect.js file because the function is called by the attributes "onclick" of your images.

With the addEventListener function, you can't get the object used, you have to use jquery framework with something like:

    var wrapper = function changeImg(param) {
    'use strict';

    var preloads=[],c;


    function preload(){
        for(c=0; c<arguments.length; c++) {
            preloads[preloads.length] = new Image();
            preloads[preloads.length-1].src = arguments[c];
        }
        c=0;
    }
    if (param == 1){
        preload('images/image10.jpg');
        // alert("param=1");
    }
    if(param == 0){
        preload('images/image7.jpg');
        // alert("param=0");
    }

    $('.dynamic-image').on('click', function() {
        c++;
        if(c == preloads.length) {
            c=0;
        }
        console.log(this.src);
        this.src = preloads[c].src;
    });
}

And your HTML file:

<!DOCTYPE html>
<html>
 <head>

 </head>
<body>
    <img src="images/image7.jpg" alt="path not found" class="dynamic-image" onclick="wrapper(1)">
    <img src="images/image10.jpg" alt="path not found" class="dynamic-image" onclick="wrapper(0)">

    <script src="proiect.js"></script>
    <script src="jquery.min.js"></script>
</body>
</html>

And of course add the "jquery.min.js" file in your folder: https://jquery.com/download/

I know that it's not exactly what you want but it can help you;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM