如何将class()添加到特定格式的图片 嗨,我只需要一个JPG格式的样式图像。(jQuery)

如何将class()添加到特定格式的图片 嗨,我只需要一个JPG格式的样式图像。(jQuery),jquery,image,format,containers,jpeg,Jquery,Image,Format,Containers,Jpeg,我需要将类应用到jpeg的特定容器中(不是页面上的所有jpg)。 例如: <img src="non-jpeg.jpg"> <!--no changes made--> <img src="jpeg-image.jpg"> <!--no changes made--> <div class="spec-container"><img src="jpeg-image.jpg"></div> <!--chang

我需要将类应用到jpeg的特定容器中(不是页面上的所有jpg)。
例如:

<img src="non-jpeg.jpg"> <!--no changes made-->
<img src="jpeg-image.jpg"> <!--no changes made-->
<div class="spec-container"><img src="jpeg-image.jpg"></div> <!--changes applied-->

像这样

<img src="non-jpeg.jpg">
<img src="jpeg-image.jpg">
<div class="spec-container"><img src="jpeg-image.jpg" class="styled"></div>  

提前谢谢

您可以使用该容器中的图像,如下所示:

$(".spec-container img[src$='.jpg']").addClass("styled");
$(".spec-container img").filter(function() {
  return /\.[jpg$|jpeg$]/i.test(this.src);
}).addClass("styled");
如果您想让选择器更宽容一些,请像这样使用
.filter()

$(".spec-container img[src$='.jpg']").addClass("styled");
$(".spec-container img").filter(function() {
  return /\.[jpg$|jpeg$]/i.test(this.src);
}).addClass("styled");
您可以使用该容器中的图像,如下所示:

$(".spec-container img[src$='.jpg']").addClass("styled");
$(".spec-container img").filter(function() {
  return /\.[jpg$|jpeg$]/i.test(this.src);
}).addClass("styled");
如果您想让选择器更宽容一些,请像这样使用
.filter()

$(".spec-container img[src$='.jpg']").addClass("styled");
$(".spec-container img").filter(function() {
  return /\.[jpg$|jpeg$]/i.test(this.src);
}).addClass("styled");

您可以使用纯CSS来实现这一点。将class=“styled”放在所有图像上,然后放在CSS中:

img.styled
{
    /* put styles that apply to non-styled images here */
}

div.spec-container img.styled
{
    /* put styles here that you want only applied to the "styled" images */
}

您可以使用纯CSS来实现这一点。将class=“styled”放在所有图像上,然后放在CSS中:

img.styled
{
    /* put styles that apply to non-styled images here */
}

div.spec-container img.styled
{
    /* put styles here that you want only applied to the "styled" images */
}

+1但是为了完整起见,可以在混合中添加
.jpeg
。还有,这是不区分大小写的吗?哦,恭喜你突破了100k的障碍@Pekka-已更新以处理各种情况,谢谢:)@dessol-您可以修改我上面的正则表达式以获得任何其他格式。您好。有必要只为大于10px的图像分配类。我如何添加此功能?非常感谢advance@dessol-请为此打开一个新问题…它与此完全无关,我们希望尝试将每个问题保留在主题上,以便以后成为更好的谷歌资源:)将我链接到此处,我当然会查看+1,但为了完整起见,可能会在混合中添加
.jpeg
。还有,这是不区分大小写的吗?哦,恭喜你突破了100k的障碍@Pekka-已更新以处理各种情况,谢谢:)@dessol-您可以修改我上面的正则表达式以获得任何其他格式。您好。有必要只为大于10px的图像分配类。我如何添加此功能?非常感谢advance@dessol-请为此打开一个新问题…它与此完全无关,我们希望尝试将每个问题保留在主题上,以便以后成为更好的谷歌资源:)将我链接到此处,我一定会查看它。。。我需要窗帘jpg格式的样式只。CSS没有为我提供这个选项。。。我需要窗帘jpg格式的样式只。CSS没有为我提供这个选项