-1

我遇到了这个片段,它的功能是用跨度包装 img 标签并制作跨度的图像背景图像。我的问题是,如何使它仅适用于某个 img 标签或某个 div 中的某些 img 标签?

$(文档).ready(函数(){

$("img").load(function() {
    $(this).wrap(function(){
        return '<span class="image-wrap ' + $(this).attr('class') + '" 
            style="position:relative; display:inline-block; background:url(' + $(this).attr('src') + ') no-repeat center center; width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px;" />';
    });
    $(this).css("opacity","0");
});

});

4

1 回答 1

0

很简单,只需$("img")用不同的选择器替换即可在不同的图像集而不是现场的所有图像上运行“包装”代码。例如$(".extra img"),用于定位标签内带有类“extra”(class="extra")的任何 img。

这是 jQuery 的基本、琐碎知识 - 我建议您阅读 jQuery 教程或更好,使用 jQuery 代码学校http://try.jquery.com/来学习如何使用 jQuery。

于 2013-05-13T07:38:49.747 回答