简体   繁体   English

单击 html 图标时单击事件不起作用

[英]click event not working when clicking html icon

I have setup a click event in Javascript which is not working for some reason, the image just isn't changing and there are no errors in the console.我在 Javascript 中设置了一个点击事件,由于某种原因它不起作用,图像没有改变,控制台中没有错误。 The image src is meant to change when clicking the html icon.单击 html 图标时,图像 src 会发生变化。

Here is the javascript:这是 javascript:

document.querySelector(".left").addEventListener("click", () => {
            leftArrow();
        });

Here is the leftArrow function:这是左箭头 function:

leftArrow: () => {
      if (document.querySelector(DOMStrings.characterImg).src === characters[0]) {
        document.querySelector(DOMStrings.characterImg).classList.add("boris");
        document.querySelector(DOMStrings.characterImg).src = characters[1];
        const el = document.querySelector(DOMStrings.characterImg);
        console.log(el);
      } else if (document.querySelector(DOMStrings.characterImg).src === characters[1]) {
        document.querySelector(DOMStrings.characterImg).classList.remove("boris");
        document.querySelector(DOMStrings.characterImg).src = characters[0];

        const el = document.querySelector(DOMStrings.characterImg);
        console.log(el);

  } 

Here is where the images are stored:这是存储图像的位置:

const characters = ["Resources/Images/trump.png", "Resources/Images/boris.png"];

Here is where the DOM strings are stored:这里是存储 DOM 字符串的地方:

const DOMstrings = {
    characterImg: ".character-img"
}

Lastly, here is the relevant html:最后,这里是相关的 html:

<div class="character-select">
                <ion-icon class="left" name="arrow-dropleft-circle"></ion-icon>
                <ion-icon class="right" name="arrow-dropright-circle"></ion-icon>
                <img class="character-img" src="Resources/Images/trump.png">        
            </div>
//below HTML part
/*
<div class="character-select">
<ion-icon class="icon" id='left' name="arrow-dropleft-circle"></ion-icon>
                <ion-icon class="icon" id='right' name="arrow-dropright-circle"></ion-icon>
                <img class="character-img" src="Resources/Images/trump.png">        
            </div>
*/
-----------------------------------------------------------------------
//javascript part
document.querySelectorAll(".icon").forEach(function(item,index){
 item.addEventListener('click',function(event){

if(event.targer.id='right'){
document.querySelector('.character-img').src='your source that you want to set'
}else if(event.targer.id='left'){
document.querySelector('.character-img').src='your source that you want to set'
}
  });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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