简体   繁体   English

当单击图像不起作用时,通过 addEventListener 进行事件

[英]Make Event by addEventListener When Click On Image does not work

In Javascript, I am trying to make event when click on img.在 Javascript 中,我试图在单击 img 时进行事件。 i use addEventListener.我使用 addEventListener。

my code what do?我的代码是做什么的? : there are six images in my page. : 我的页面中有六张图片。 and every image has two classes.每个图像都有两个类。 the first class is 'img-class' and all the images have it.第一个 class 是“img-class”,所有图像都有它。

and the second class is 'id' word and number.第二个 class 是“id”字和数字。 for example: the second class of image one is 'id1', and the second class of image two is 'id2'.例如:图像一的第二个 class 是“id1”,图像二的第二个 class 是“id2”。

what i want to do: i want when i click on any image from the six image the console print the second id of the image which i clicked.我想要做什么:我希望当我单击六个图像中的任何图像时,控制台打印我单击的图像的第二个 id。

the problem is 'no thing happen when i click on the image, no print error in the console no thing happen'.问题是“当我单击图像时没有任何事情发生,控制台中没有打印错误没有任何事情发生”。

i tried to get the second class without click event and work.我试图在没有点击事件和工作的情况下获得第二个 class。

so the problem in event function.所以事件function中的问题。

where is the problem?问题出在哪里?

this is javascript code:这是 javascript 代码:

var Img_Img = document.querySelectorAll('.img-class');

    Img_Img.forEach(Img_Img_Img => {
    
        Img_Img_Img.addEventListener("click" , ()=>{
            if (Img_Img_Img.classList.contains("imgimg") != null) {
                Img_Img_Img.classList.remove("imgimg");
            }
        
            if (Img_Img_Img.classList.contains("imgimg") == null) {
                Img_Img_Img.classList.add("imgimg");
            }
    
    
            document.querySelectorAll('.imgimg').forEach( imgimgimg => {
                var idid = imgimgimg.getAttribute('class').split(' ')[1];
                console.log(idid);
                
            });
    
            });
    });

html code: html 代码:

  <div class = "images-images">
                <div>
                        <img src = "Images/watch.jpg" class = "img-class id1">
                    <div class = "overlay-image-image"></div>
                </div>

                <div>
                    <img src = "Images/watch_2.jpg" class = "img-class id2">
                    <div class = "overlay-image-image"></div>
                </div>

                <div>
                    <img src = "Images/card.jpg" class = "img-class id3">
                    <div class = "overlay-image-image"></div>
                </div>

                <div>
                    <img src = "Images/shoes.jpg" class = "img-class id4">
                    <div class = "overlay-image-image"></div>
                </div>

                <div>
                    <img src = "Images/card_2.jpg" class = "img-class id5">
                    <div class = "overlay-image-image"></div>
                </div>

                <div>
                    <img src = "Images/headphones.jpg" class = "img-class id6">
                    <div class = "overlay-image-image"></div>
                </div>
   </div>

You can use the substring method to get only part of a string.您可以使用 substring 方法仅获取字符串的一部分。

 const images = document.querySelectorAll('.img-class'); images.forEach(function (image) {image.addEventListener('click', () => console.log(image.className.substring(10, 13)))})
 <div class = "images-images"> <div> <img src = "Images/watch.jpg" class = "img-class id1"> <div class = "overlay-image-image"></div> <div> <img src = "Images/watch_2.jpg" class = "img-class id2"> <div class = "overlay-image-image"></div> </div> </div>

Img_Img_Img.classList.contains() method always return boolean value. Img_Img_Img.classList.contains() 方法总是返回 boolean 值。 So your code should be follows.所以你的代码应该如下。

if (Img_Img_Img.classList.contains("imgimg") == false) {
    Img_Img_Img.classList.add("imgimg");             
}

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

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