简体   繁体   中英

Javascript click event not reverting according to my statement

I'm trying to revert my click event after clicking but my else statement seems not to be working, I do not know if something is wrong with my code:

var img = document.querySelector ('.change');

img.addEventListener('click', function(){
    if(img.src = 'homepage imgs/inactive.svg') {
        img.src = 'homepage imgs/like icon.svg';
    } else{
        img.src = 'homepage imgs/inactive.svg';
    }
});

you are actually assigning rather than comparing a single = is used to assign like

a = 10;

a double == is used for comparison, like whether a is equal to 10 or not

like a==10;

to fix your issue change to

if(img.src == 'homepage imgs/inactive.svg') 

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