通过JS更改所有图像的img url

时间:2017-12-28 19:44:45

标签: javascript jquery

我有一个图像列表,具有这种结构:

<div class="product">
    <div class="p" itemscope="" itemtype="https://schema.org/Product">
        <a href="/head/" class="p-image">
            <img src="https://cdn.myshoptet.com/usr/www.nebenebo.cz/user/shop/detail/348.jpg?5a1af136" data-src="https://cdn.myshoptet.com/usr/www.nebenebo.cz/user/shop/detail/348.jpg?5a1af136" alt="69Jirka Macek Head">
            <meta itemprop="image" content="https://cdn.myshoptet.com/usr/www.nebenebo.cz/user/shop/big/348.jpg?5a1af136">
        </a>
    </div>
</div>

使用JS,我需要在特定类的所有img上将路径文件夹从"detail/"更改为"big/"

我尝试了这个,但它不起作用:

$('.product a img').attr('src').replace('detail', 'big');

1 个答案:

答案 0 :(得分:0)

//if you want to change the "detail" to "big" for all images, 
$('.product a img').each(function(){
  $(this).attr('src', $(this).attr('src').replace('detail', 'big'));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
<div class="p" itemscope="" itemtype="https://schema.org/Product">
<a href="/head/" class="p-image">
<img src="https://cdn.myshoptet.com/usr/www.nebenebo.cz/user/shop/detail/348.jpg?5a1af136" data-src="https://cdn.myshoptet.com/usr/www.nebenebo.cz/user/shop/detail/348.jpg?5a1af136" alt="69Jirka Macek Head">
                    <meta itemprop="image" content="https://cdn.myshoptet.com/usr/www.nebenebo.cz/user/shop/big/348.jpg?5a1af136">
    </a>   
  </div>
 </div>