简体   繁体   English

单击仅在第二次单击时起作用 javascript

[英]click only work on second click javascript

could you help me on this issue?你能帮我解决这个问题吗? Im trying to get the value of the img src on click of the tag with classname downloadModal.我试图通过单击具有类名 downloadModal 的标签来获取 img src 的值。 I have tried several ways and sometimes it does work but only one the second click, and sometimes it doesn't work at all.我尝试了几种方法,有时它确实有效,但只有一次单击,有时它根本不起作用。 The tag with className downloadModal isn't in the source code and is built with javascript.带有类名 downloadModal 的标签不在源代码中,它是用 javascript 构建的。 Any idea how could i get it?知道我怎么能得到它吗? Thank u so much非常感谢你

THE CLICK FUNCTION点击 FUNCTION

 //get img source name from src link
    var imageDetails = "";
    function getImageName(){
        var storage_link = $(this).find('img').attr('src');
        var imageName = storage_link.split("/")[5];
        var imageName = imageName.split(".")[0];
        imageDetails = imageName;
      }

THE TAG CODE标签代码

var divImageList = document.getElementById("imageList")
  listImage.forEach(function (image) {
    var div = document.createElement('div');
    divImageList.appendChild(div)
    var className = ""
    model__filterList.forEach(function (typeGroup) {
      className += image[typeGroup.type] + " ";
    });
    className = "DownloadModal filterDiv" + image.id + image.className;
    div.setAttribute("class", className)
    div.setAttribute("id", "icon-all-" + image.id);
    div.setAttribute("onclick", "ctrl__openModal('" + image.id + "'); getImageName();")

THE DISPLAYED CODE显示的代码

]

You forgot to use += to add to the class string, instead you overwrite it immediately with = after the loop.您忘记使用+=添加到 class 字符串,而是在循环后立即用=覆盖它。
This should work instead:这应该起作用:

    var className = "";
    model__filterList.forEach(function (typeGroup) {
      className += image[typeGroup.type] + " ";
    });
    className += "DownloadModal filterDiv" + image.id + image.className;

You also wanted to write the last statement as this I am assuming?您还想像我假设的那样写最后一条语句?

className += "DownloadModal filterDiv " + image.id + " " + image.className;

It seems you forgot to add the spaces there - except this was your intention, of course.您似乎忘记在此处添加空格 - 当然,这是您的意图。

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

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