如何给某种格式的图片添加addClass()

发布于 2024-09-29 13:56:36 字数 570 浏览 0 评论 0原文

你好,我只需要设置 JPG 格式图像的样式。 (jQuery)

当 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> <!--changes applied-->

像这样

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

提前致谢!

Hi, I need to style images of a JPG format only. (jQuery)

I need class to be applied to jpeg's when it's placed in specific container (not all jpg's on the page).
For example:

<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-->

to be like this

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

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2024-10-06 13:56:36

您可以对其中的图像使用 attribute-ends-with 选择器 来完成此操作容器,如下所示:

$(".spec-container img[src$='.jpg']").addClass("styled");

如果您希望选择器更宽容一些,请使用 .filter() ,如下所示:

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

You can do it using an attribute-ends-with selector for images inside that container, like this:

$(".spec-container img[src$='.jpg']").addClass("styled");

If you want the selector a bit more forgiving, use .filter() like this:

$(".spec-container img").filter(function() {
  return /\.[jpg$|jpeg$]/i.test(this.src);
}).addClass("styled");
A君 2024-10-06 13:56:36

你可以用纯 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 */
}

You can just do that with pure CSS. Put the class="styled" on all your images, then in the 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 */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文