[英]click only work on second click javascript
你能帮我解决这个问题吗? 我试图通过单击具有类名 downloadModal 的标签来获取 img src 的值。 我尝试了几种方法,有时它确实有效,但只有一次单击,有时它根本不起作用。 带有类名 downloadModal 的标签不在源代码中,它是用 javascript 构建的。 知道我怎么能得到它吗? 非常感谢你
点击 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;
}
标签代码
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();")
显示的代码
您忘记使用+=
添加到 class 字符串,而是在循环后立即用=
覆盖它。
这应该起作用:
var className = "";
model__filterList.forEach(function (typeGroup) {
className += image[typeGroup.type] + " ";
});
className += "DownloadModal filterDiv" + image.id + image.className;
您还想像我假设的那样写最后一条语句?
className += "DownloadModal filterDiv " + image.id + " " + image.className;
您似乎忘记在此处添加空格 - 当然,这是您的意图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.