[英]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.