如何使一个Jquery片段只适用于某些特定的标签
How to make a Jquery snippet only apply to some certain tags?
我遇到了这个代码片段,它的功能是用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。
相关文章:
- JQueryhide()不适用于Mozzilla,但适用于Chrome
- PHP中的setcookie仅适用于localhost
- html5 drawImage适用于firefox,而不是chrome
- 提供“;onClick"适用于iPad(触摸屏)和桌面用户的默认功能
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- ResolveUrl是否适用于././也
- Firebase updateChildValues适用于IOS,但不适用于Web和Android
- JavaScript警报适用于int,但不适用于string
- SQLite插件适用于Mac和Windows,但不适用于手机上的Safari
- Regex Replace仅适用于Last Match
- window.onload适用于aspx页面,但不适用于普通html
- Jquery Ajax POST不工作.适用于GET
- 画布上的自定义字体仅适用于safari
- Javascript仅适用于alert()和Debug模式
- JS适用于Firefox和Safari,但不适用于Chrome.此处'是我的网站
- Regex不适用于Firefox,但适用于Chrome
- HTML5视频标签没有't适用于iPad
- Javascript代码关闭标签,适用于IE, Firefox和Chrome
- 从标签返回文本,其中最外层的HTML标签适用于jquery中的所有文本节点
- Onclick只适用于img标签