【问题标题】:jQuery add alt tag to images with same srcjQuery将alt标签添加到具有相同src的图像
【发布时间】:2018-09-10 16:12:45
【问题描述】:

有很多图片具有相同的 URL 来源,但只有第一张图片有 Alt 标签,那么如何将 alt 添加到具有相同来源的另一张图片?

$(function(){
    var srcs = [],
        alt = '',
        title = '',
        temp;
    $("img").filter(function(){
        temp = $(this).attr("src");
        alt = $(this).attr("alt");
        title += $(this).attr("title");

        if($.inArray(temp, srcs) < 0){
            srcs.push(temp);
            srcs.push(alt);
            srcs.push(title); 
            return false;
        }
        return true;
    }).attr('alt',alt);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="colourDots">
   <img alt="aaa" title="axxx" src="/out/pictures/generated/product/8/300_450_100/60028.bl.jpg"/><br>
   <img src="/out/pictures/generated/product/8/300_450_100/60028.bl.jpg"/><br>
   <img  alt="bbb" title="axxx" src="/out/pictures/generated/product/8/300_450_100/60028.bl.jpg"/><br>
   <img src="/out/pictures/generated/product/8/300_450_100/60028.sw.jpg"/><br>
   <img src="/out/pictures/generated/product/8/300_450_100/60028.bl.jpg"/><br>
   <img src="/out/pictures/generated/product/8/300_450_100/60028.bl.jpg"/><br>
   <img src="/out/pictures/generated/product/8/300_450_100/60028.bl.jpg"/><br>
</div>

我只需要过滤图像即可

具有相同源 URL 的所有图像。

将alt标签复制到其他没有alt但来源网址相同的图片

【问题讨论】:

  • 我不确定我是否理解,您的问题是“如何将alt 属性添加到现在缺少这些属性的图像中”?因为到目前为止,最好的地方是生成所有这些 img 元素标签。
  • @Mike'Pomax'Kamermans 我只需要过滤图像以获取所有具有相同源 URL 的图像,然后将 alt 标签复制到其他没有 alt 但具有相同源 URL 的图像
  • 查找所有图像,并将该列表减少到所有唯一的src。然后对于每个唯一的 src,获取具有该来源的所有图像,子选择具有 alt 的元素,然后设置所有图像(包括您刚刚找到的图像)以使用该 alt?

标签: javascript jquery html filter


【解决方案1】:
  1. 获取所有唯一的 src 属性,
  2. 对于每个唯一的 src 属性,获取所有使用它的图像
  3. 对于每个集合,找到具有alt 的成员,然后将其分配给整个集合。

在代码中:

const uniques = [];
const imgs = $('img');

imgs.each(function () {
  let src = this.src;
  if (uniques.indexOf(src) === -1) uniques.push(src);
});

uniques.forEach(src => {
  let srcd = imgs.filter("img[src='"+src+"']");
  let alt = srcd.filter('[alt]').first().attr('alt');

  srcd.each( function() {
    $(this).attr('alt',alt);
  })
});

【讨论】:

  • 不工作。需要图像具有相同的来源获得相同的alt也
  • ... 这就是这段代码的作用。对于每个已知的src 字符串,它会找到具有该 src 的 all 图像,然后确保它们都以相同的alt 属性结束,所以如果这不起作用,请说出什么是不工作。 “不工作”不是解决问题的有用信息,它只是一种意见(计算机完全按照您的指示行事)
  • 我已经编辑了选择器变量逗号然后尝试但是没有定义uniqueSrc,你在jsfiddle中尝试过吗?
  • 这是一个粘贴错误,如果你提到 uniqueSrc 给出了一个错误,我可以更早地修复它 =) 更新了文本。
  • 更新代码但不做任何效果见这里jsfiddle.net/earngate/xkhvjjqp
【解决方案2】:

第一个版本(使用 cmets)仅将 alt 的第一个实例的值分配给所有图像:

$(function () {
    // Get value of first instance of 'alt' for images within elements of the given class
    var alt = $(".colourDots img[alt]").first().attr("alt");
    // If value of 'alt' is not null or undefined
    // (Note, use != and not !== for this test)
    if (alt != null) {
        // Iterate images within elements of class .colourDots
        $.each($(".colourDots img"), function () {
            // assign value of var alt to each image
            $(this).attr("alt", alt);
        });
    }
});

第二个版本将元素的alt 值分配给后续图像(因此您的问题中的alt="bbb" 被拾取并分配给后续图像):

$(function () {
    // Define a holding variable
    var alt;
    // Iterate images
    $.each($(".colourDots img"), function () {
        // If element has 'alt', assign the value to var alt
        if ($(this).attr("alt") != null) {
            alt = $(this).attr("alt");
        }
        // otherwise assign the value of var alt to the image
        else {
            $(this).attr("alt", alt);
        }
    });
});

您可以根据自己的具体需求进行选择。

【讨论】:

  • 检查我的代码,我认为您可以通过仅在没有 alt 标签的标签时运行循环来优化您的代码。
  • 谢谢,但您的代码是否不必首先迭代图像才能找到那些没有 alt 属性的图像?
  • 我们应该如何解决它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多