我试图将div(.portrait_text
)添加到容器(.gallery_container
)中:
每个图片都会预先添加到相应的.gallery_container
。但是我想将每个下一个 .portrait_text
div添加到.gallery_container
之前。有什么想法吗?
<div id="gallery_images_inner" class="clearfix">
<img src="img/portrait.jpg" alt="portrait" width="850" height="557">
<div class="portrait_text">Girls After Swim</div>
<img src="img/gallery5.jpg" alt="gallery5" width="850" height="566">
<div class="portrait_text">Girls After Swim</div>
</div>
JS:
function Gallery(selector) {
this.add_module = function (type, image) {
var container = $('<div />', {
'class': 'gallery_container'
}).append(image);
if (type == 'horizontal') {
var h_ar = image.attr('height') / image.attr('width');
var c_width = selector.width();
var c_height = selector.width() * h_ar
container.css({
'width': c_width - 60,
'height': c_height
})
}
if (type == 'vertical') {
var c_width = v_width;
var c_height = v_height
container.css({
'width': Math.floor(v_width) - 30,
'height': v_height
})
}
container.css({
'float': 'left',
'overflow': 'hidden'
})
container.find('img').attr({
'width': '100%',
'height': '100%'
})
container.attr('ar', c_height / c_width)
container.appendTo(selector);
//container.children('img').fitToBox();
}
}
答案 0 :(得分:0)
jQuery有next selector,所以你可以做
$(selector).next(".portrait_text").appendTo(container);
答案 1 :(得分:0)
如果image
作为jQuery对象传递,正如您的代码似乎建议的那样,您也可以使用.next()
来选择文本:
function Gallery(selector) {
this.add_module = function (type, image) {
var portrait_text = image.next('.portrait_text');
var container = $('<div />', {
'class': 'gallery_container'
}).append(image).append(portrait_text);
...
}
}
当然,这是追加,您要求预先,但概念是相同的。