【问题标题】:jQuery Wildcare Image SourcejQuery Wildcare 图像源
【发布时间】:2015-05-27 09:49:48
【问题描述】:

我正在尝试更改图像 src 的一部分以进行 A/B 测试,但在阅读了大量文章后,我越来越困惑。这是图像 URL(它是结果列表项的一部分):

http://pic.test.net/images-small/001/002/123456.jpg

我必须将 /image-small/ 更改为 /image-big/。

部分 URL 是动态的(例如 /001/),所以我的想法是为这些部分使用某种通配符。这是我的静态 URL 版本:

$("#resultlist-item").attr({"src":"http://pic.test.net/images-big/001/002/123456.jpg"});

它正在工作,但我不知道如何为动态 URL 执行此操作。

有什么想法吗?

非常感谢, 乔

【问题讨论】:

  • 动态url是什么意思?
  • @Satpal 基本上图片是广告的一部分,我通过 API 获取详细信息。所以图片网址中的数字可能会有所不同,但第一部分“pic.test.net/images-small”始终相同。

标签: javascript jquery image wildcard


【解决方案1】:

试试这个:

<!-- SOME HTML AND ETC... -->   
<div id="gallery">
    <img src="http://pic.test.net/images-small/001/002/123456.jpg" />
    <img src="http://pic.test.net/images-small/001/002/1234567.jpg" />
    <img src="http://pic.test.net/images-small/001/002/1234568.jpg" />
</div>
<!-- SOME HTML AND ETC... --> 

<script>
$(function(){
    var $imagesContainer = $('#gallery');
    $imagesContainer.find('img').each(function(){
        var src = $(this).attr('src');
        if(src.indexOf('images-small/')>-1) {
            src = src.replace('images-small/', 'images-big/');
            $(this).attr('src', src);
        }
    });

});
</script>

【讨论】:

  • 有效!谢谢@num8er
猜你喜欢
  • 2014-02-10
  • 2010-10-07
  • 2012-02-20
  • 2017-10-13
  • 2011-05-24
  • 2011-07-11
  • 2012-06-23
  • 1970-01-01
  • 2012-12-20
相关资源
最近更新 更多