英文:
how can i add another image to this function?
问题
这个函数基本上是当我点击myBtn时,图片会变成另一张图片,我已经实现了这个功能,但是如何添加另一张我想要更改的图片,并在点击时同时更改这两张图片,有人可以帮忙吗?
const myBtn = document.getElementById("darktheme");
const img = document.getElementById("ferrarijs");
let mySrc = img.getAttribute("src");
let counter = 0;
myBtn.addEventListener("click", function() {
if (counter % 2 === 0) {
img.setAttribute("src", "images/ferrari.jpg");
} else {
img.setAttribute("src", "images/ferrariwhte.jpg");
}
counter++;
});
英文:
so this function is basically when i click on myBtn the img change to another img, i made that but how can i add another img i wanna change and change both the imgs when i click, can anyone help
const myBtn = document.getElementById("darktheme");
const img = document.getElementById("ferrarijs")
let mySrc = img.getAttribute("src");
let counter = 0;
myBtn.addEventListener("click", function() {
if (counter % 2 === 0) {
img.setAttribute("src", "images/ferrari.jpg");
} else {
img.setAttribute("src", "images/ferrariwhte.jpg");
}
counter++;
});
答案1
得分: 1
你需要使用图像URL数组的长度来计算下一个索引。
const images = [
'http://placekitten.com/200/300',
'http://placekitten.com/300/300',
'http://placekitten.com/300/200',
];
const myBtn = document.getElementById('darktheme');
const img = document.getElementById('ferrarijs')
let counter = 0;
myBtn.addEventListener('click', function() {
counter = (counter + 1) % images.length;
img.src = images[counter];
});
body { display: flex; align-items: flex-start; justify-content: center; }
<img id="ferrarijs" src="http://placekitten.com/200/300" />
<button id="darktheme">Next</button>
英文:
You will need to compute the next index by using the length of an image URL array.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const images = [
'http://placekitten.com/200/300',
'http://placekitten.com/300/300',
'http://placekitten.com/300/200',
];
const myBtn = document.getElementById('darktheme');
const img = document.getElementById('ferrarijs')
let counter = 0;
myBtn.addEventListener('click', function() {
counter = (counter + 1) % images.length;
img.src = images0+网站访问量;
});
<!-- language: lang-css -->
body { display: flex; align-items: flex-start; justify-content: center; }
<!-- language: lang-html -->
<img id="ferrarijs" src="http://placekitten.com/200/300" />
<button id="darktheme">Next</button>
<!-- end snippet -->
答案2
得分: 0
这很容易。只需复制并粘贴,就完成了。
const myBtn = document.getElementById("darktheme");
const img = document.getElementById("ferrarijs");
const img2 = document.getElementById("teslajs");
let mySrc = img.getAttribute("src");
let counter = 0;
myBtn.addEventListener("click", function() {
if (counter % 2 === 0) {
img.setAttribute("src", "images/ferrari.jpg");
} else {
img.setAttribute("src", "images/ferrariwhte.jpg");
}
if (counter % 2 === 0) {
img2.setAttribute("src", "images/tesla.jpg");
} else {
img2.setAttribute("src", "images/teslawhte.jpg");
}
counter++;
});
这将在<img src="teslawhite.jpg" src="teslajs">
之间切换tesla
和teslawhite
的src。我知道"Tesla"是一个相当愚蠢的例子。随意用您想要的内容进行更改。
英文:
It's pretty easy. That's just copy & paste & you're done.
const myBtn = document.getElementById("darktheme");
const img = document.getElementById("ferrarijs")
const img2 = document.getElementById("teslajs")
let mySrc = img.getAttribute("src");
let counter = 0;
myBtn.addEventListener("click", function() {
if (counter % 2 === 0) {
img.setAttribute("src", "images/ferrari.jpg");
} else {
img.setAttribute("src", "images/ferrariwhte.jpg");
}
if (counter % 2 === 0) {
img2.setAttribute("src", "images/tesla.jpg");
} else {
img2.setAttribute("src", "images/teslawhte.jpg");
}
counter++;
});
This will toggle the src of <img src="teslawhite.jpg" src="teslajs">
between tesla
& teslawhite
. I know "Tesla" is a pretty dumb example. Feel free to change it with what you want.
答案3
得分: 0
你可以使用 querySelectorAll
来查找所有的 <img>
。
然后循环遍历它们以设置 src
。
使用一个数组来获取下一个图片的 src
,检查当前索引是否匹配 length - 1
,如果是的话,从索引 0 开始。
const srcs = [
'https://placehold.co/150?text=Image-0',
'https://placehold.co/150?text=Image-1',
'https://placehold.co/150?text=Image-2',
'https://placehold.co/150?text=Image-3'
];
const myBtn = document.getElementById("darktheme");
const imgs = [...document.querySelectorAll("img")];
myBtn.addEventListener("click", function () {
imgs.forEach(i => {
let oldIndex = srcs.findIndex(e => e === i.src);
let newIndex = (oldIndex === srcs.length - 1) ? 0 : oldIndex + 1;
i.src = srcs[newIndex];
});
});
<button id='darktheme'>Click me!</button>
<img src='https://placehold.co/150?text=Image-0' />
<img src='https://placehold.co/150?text=Image-0' />
英文:
You can use querySelectorAll
to find all <img>
's.
Then loop over those to set the src
.
Use an array to get the next image src
, check if the current index matches length - 1
, if so, start again at index 0
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const srcs = [
'https://placehold.co/150?text=Image-0',
'https://placehold.co/150?text=Image-1',
'https://placehold.co/150?text=Image-2',
'https://placehold.co/150?text=Image-3'
];
const myBtn = document.getElementById("darktheme");
const imgs = [ ...document.querySelectorAll("img") ];
myBtn.addEventListener("click", function() {
imgs.forEach(i => {
let oldIndex = srcs.findIndex(e => e === i.src);
let newIndex = (oldIndex === srcs.length - 1) ? 0 : oldIndex + 1;
i.src = srcs[newIndex];
});
});
<!-- language: lang-html -->
<button id='darktheme'>Click me!</button>
<img src='https://placehold.co/150?text=Image-0' />
<img src='https://placehold.co/150?text=Image-0' />
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论