如何使一个Jquery片段只适用于某些特定的标签

How to make a Jquery snippet only apply to some certain tags?

本文关键字:标签 适用于 何使一 Jquery 片段      更新时间:2023-09-26

我遇到了这个代码片段,它的功能是用span包装img标记,并使该span的图像背景图像。我的问题是,如何让它只适用于某个img标签或某个div中的某些img标签?

文档

$()时()函数{

$("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");
});

});

简单,只需将$("img")替换为不同的选择器,在不同的图像集上运行"包装"代码,而不是在站点上的所有图像。例如,使用$(".extra img")来定位任何在标签内的图片,class="extra" (class="extra")。

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