gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:37:37 24 4
gpt4 key购买 nike

我有一段简单的 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 通过单击代码片段顶部的按钮来设置图像源。

例如 - 当页面首次加载时,图像 url 被硬编码为:

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 之间进行更改。

最佳答案

解决问题的一个简单方法是为每个按钮指定一个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 选择所有这些按钮,然后将监听器添加到单击事件,并通过选择所有图像来更改所有图像的 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">

关于javascript - 使用按钮替换多个图像的 img src 的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51000737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com