-2

我需要一些 JS/jquery(首选 jquery)的帮助,它们可以替换站点上与特定模式匹配的所有图像。在页面上,我们的图像看起来像:

img src="http://a248.e.akamai.net/a/262/9086/10h/origin-d9.scene7.com/is/image/default/**A34567_v1**?wid=570&hei=413&fmt=jpeg&qlt=92,0&resMode=sharp2&op_usm=1.1,0.5,1,0"

如果图像 src 包含“_v1”,我只想将该字符串更改为“_v2”。使用上面的示例,最终结果将是:

img src="http://a248.e.akamai.net/a/262/9086/10h/origin-d9.scene7.com/is/image/default/**A34567_v2**?wid=570&hei=413&fmt=jpeg&qlt=92,0&resMode=sharp2&op_usm=1.1,0.5,1,0"

这可能在一个页面上发生多次。有人可以帮忙吗?

谢谢!

4

2 回答 2

1

由于您没有提供多个示例,因此我无法确定您要使用哪种模式。但是,我创建了一个带有正则表达式的简化版本,以便为您指明正确的方向:

考虑这个 HTML:您想将所有旧的更改为新的。

<img src="/old_a.png" />
<img src="/new_b.png" />
<img src="/old_c.png" />

您将使用 jQuery 遍历所有 img 标签,如下所示:

$("img").each(function(i, element){
    var pattern = /[old]/; //A regular expression that you'll have to modify to your needs
    var src = $(element).attr("src");
    if(pattern.test(src))
        $(element).attr("src", src.replace("old", "new"));
});

提琴手

基本上,它的作用是将正则表达式应用于每个图像元素。但是,您必须自己创建该正则表达式。

于 2013-05-02T15:32:02.850 回答
0

这可能会做到:

$(function(){

    $('img').each(function(index, img){
        $(this).attr('src', $(this).attr('src').replace('_v1**', '_v2**'));
    });

});
于 2013-05-02T15:21:35.057 回答