我正在尝试使用jQuery mobile输出带有关联弹出窗口的图像,我在循环中输入以下代码:
for( var i = 0, c = imageArray.length; i < c; i++ ) {
counter ++;
// Create new Image element
var img = $('<img data-rel="popup" class="image"/>').attr({src: imageArray[i], href: "#"+"image"+counter});
// Append new img to our photos div
img.appendTo('.photos');
var popoutID = "image"+counter;
var popout = $('<div data-role="popup"></div>').attr("id", popoutID);
popout.appendTo('.photos');
var popoutImg = $('<img />').attr("src", imageArray[i]);
popoutImg.appendTo(popoutID);
}
这会正确输出图像,popout div具有正确的ID,但是当我尝试将图像附加到弹出窗口时,Web检查器会将其显示为空?
答案 0 :(得分:2)
appendTo
指向一个字符串,而不是popout:
popoutImg.appendTo(popout);
我想在变量前面添加一个$
作为提醒,所以我知道它是一个jQuery对象:
var $popout = $('<div data-role="popup"></div>').attr("id", popoutID);
$popout.appendTo('.photos');
var popoutImg = $('<img />').attr("src", imageArray[i]);
popoutImg.appendTo($popout);