使用按钮将img src的一部分替换为多个图像

时间:2018-06-23 11:30:52

标签: javascript html

我有一些简单的HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<button href="#" class="btn btn-primary">Small (32)</button>
<button href="#" class="btn btn-danger">Medium (64)</button>
<button href="#" class="btn btn-warning">Large (128)</button>

<hr />

<img class="swap" title="jack-o-lantern" alt="jack-o-lantern" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/1f383.png">
<img class="swap" title="Christmas tree" alt="Christmas tree" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/1f384.png">
<img class="swap" title="fireworks" alt="fireworks" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/1f386.png">
<img class="swap" title="sparkler" alt="sparkler" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/1f387.png">
<img class="swap" title="sparkles" alt="sparkles" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/2728.png">

我正在尝试确定是否可以通过使用代码段顶部的按钮来使用javascript设置图像源。

例如-首次加载页面时,图片网址被硬编码为:

https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/1f383.png

但是,如果用户单击Small (32)按钮,是否可以在不将页面加载到例如页面的情况下更新所有图像源URL。一张图片:

https://cdn.jsdelivr.net/emojione/assets/3.1/png/32/1f383.png

与“中”和“大”按钮等效吗?

我搜索了例如替换img src的一部分,并找到了这篇文章:

Changing part of image's source

使用此解决方案:

$(function(){
  $('#myLink').click(function(){
    $('img').each(function(){
      var $this = $(this)
      $this.attr('src',$this.attr('src').replace('gray','green'))
    })
  })
})

但是-在我的情况下,我不仅会用另一个值替换一个值,而且还需要根据单击哪个按钮在32、64和128之间进行更改。

1 个答案:

答案 0 :(得分:1)

一个简单的解决方案是为每个按钮提供一个 id ,以表示要更改此大小的数字,例如此示例

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<button href="#" id="32" class="btn btn-primary">Small (32)</button>
<button href="#" id="64" class="btn btn-danger">Medium (64)</button>
<button href="#" id="128" class="btn btn-warning">Large (128)</button>

现在添加一个脚本,该脚本通过其ID选择所有这些按钮,然后将侦听器添加到click事件,并通过选择所有图像来更改所有图像的src属性

这是完整的解决方案

const small = document.getElementById('32');
const medium = document.getElementById('64');
const large = document.getElementById('128');

// select all images
const allImages = document.querySelectorAll('.swap') ;

// this function takes number and change the src of all images
function changeImagesSrc(number) {
    allImages.forEach(img=>{
        // here we use regular expression to select the number and change it to a new one
        img.src = img.src.replace(/32|64|128/,number);
    })
}

small.addEventListener('click',()=>{
    changeImagesSrc(32);
})

medium.addEventListener('click',()=>{
    changeImagesSrc(64);
})
large.addEventListener('click',()=>{
    changeImagesSrc(128);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<button href="#" id="32" class="btn btn-primary">Small (32)</button>
<button href="#" id="64" class="btn btn-danger">Medium (64)</button>
<button href="#" id="128" class="btn btn-warning">Large (128)</button>

<hr />

<img class="swap" title="jack-o-lantern" alt="jack-o-lantern" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/1f383.png">
<img class="swap" title="Christmas tree" alt="Christmas tree" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/1f384.png">
<img class="swap" title="fireworks" alt="fireworks" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/1f386.png">
<img class="swap" title="sparkler" alt="sparkler" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/1f387.png">
<img class="swap" title="sparkles" alt="sparkles" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/128/2728.png">