[英]Modal doesn't work
我使用jQuery制作了一個模式,但它似乎無法正常工作。 我正在使用javascript函數調用圖像,並且模態位於我的window.onload圖像加載頁面中。 我先加載圖像,但只有將imgs直接放在HTML中並且從window.onload中獲取其他所有內容時,模態模式才有效。 這就是我叫所有img的方式:
function maisGaleria(n){
var galeria = new Array();
var img = document.querySelector("#gallery");
var pic = 0;
if(n == 1){
pic = 4;
}else if(n == 2){
pic = 7;
}else{
pic = 4;
}
img.innerHTML = "<div class='row'>";
img.innerHTML += "<div class='six columns'>";
img.innerHTML += "<h4>Galeria "+n+"</h4>";
for(var i = 0; i < pic; i++){
galeria[i] = "foto"+n+"_"+(i+1)+".jpg";
}
for(var i = 0; i < galeria.length; i++){
img.innerHTML += "<img src='img/"+galeria[i]+"' class='imgs-modal'>";
}
img.innerHTML += "</div>";
img.innerHTML += "</div>";
}
和使用模態的window.onload:
window.onload = function(){
var rdSocial = document.querySelector("#social");
var teste = document.querySelector("#teste");
var fb = '<img src="img/fb.png" value="1" class="img" onclick="redireciona(this);">';
var twt = '<img src="img/twitter.png" value="2" class="img" onclick="redireciona(this);">';
var insta = '<img src="img/insta.png" value="3" class="img" onclick="redireciona(this);">';
rdSocial.innerHTML += fb;
rdSocial.innerHTML += twt;
rdSocial.innerHTML += insta;
var x = location.search.split("?gal=")[1];
y = document.querySelector("#pics");
//console.log(x);
y.onload = maisGaleria(x);
//the modal starts here
$('.imgs-modal').on('click', function(){
var obj = $(this).clone();
console.log(obj);
$("#box_modal").html(obj);
$("#modal").fadeIn(1000);
});
$(".fechar").on('click', function(){
$("#modal").fadeOut(1000);
});
}
@anuseranother您的代碼中幾乎沒有錯誤。
在window.onload中調用maisGaleria方法之前,必須聲明/定義它。
根據您的jsfiddle,它會拋出未定義maisGaleria的錯誤。 因此,請在使用前定義maisGaleria。
解決此錯誤后,另一個錯誤是“無法將屬性'onload'設置為null”。 dom中沒有#pics元素(y = document.querySelector(“#pics”)),您正在引用它並向其添加onload方法。 請更新這兩個,並讓我們確切地知道您如何需要一個帶有圖像的模態,無論是互聯網上的任何示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.