使用jquery为基于alt文本的一些图像添加一个类

adding a class to some images based on the alt text using jquery?

本文关键字:添加 图像 一个 使用 alt 文本 jquery      更新时间:2023-09-26

如何使用jquery将类添加到基于alt文本的一些图像中?

这是一个图像示例:

<img border="0" src="images/Product/icon/26086_1_.jpg" 
alt="Show Picture 1" onclick="setcolorpicidx_26086(1);" 
style="cursor:hand;cursor:pointer;">

因此,如果alt文本包含"显示图片",则添加一类image-nav

您也可以使用过滤器:

$('img').filter(function() {
    return $(this).prop('alt').indexOf('Show Picture')!=-1;
}).addClass('image-nav');
$('img[alt^="Show Picture"]').addClass('image-nav');

使用从选择器开始的属性

$('img[alt*="Show Picture"]').addClass('image-nav');

使用属性包含选择器

要使用以选择器[name^="value"] 开头的属性

$('img[alt^="Show Picture"]').addClass("image-nav");