jQuery Wildcare Image Source

jQuery Wildcare Image Source

本文关键字:Source Image Wildcare jQuery      更新时间:2023-09-26

我正试图为a/B测试改变图像src的一部分,但在阅读了大量文章后,我越来越困惑。这是图像URL(它是resultlist-item的一部分):

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做它。

任何想法?

很多谢谢,乔

试试这个:

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