如何将另一张图片添加到这个函数?

huangapple go评论67阅读模式
英文:

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 = [
  &#39;http://placekitten.com/200/300&#39;,
  &#39;http://placekitten.com/300/300&#39;,
  &#39;http://placekitten.com/300/200&#39;,
];

const myBtn = document.getElementById(&#39;darktheme&#39;);
const img = document.getElementById(&#39;ferrarijs&#39;)
let counter = 0;

myBtn.addEventListener(&#39;click&#39;, function() {
  counter = (counter + 1) % images.length;
  img.src = images
0
+
网站访问量
; });

<!-- language: lang-css -->

body { display: flex; align-items: flex-start; justify-content: center; }

<!-- language: lang-html -->

&lt;img id=&quot;ferrarijs&quot; src=&quot;http://placekitten.com/200/300&quot; /&gt;
&lt;button id=&quot;darktheme&quot;&gt;Next&lt;/button&gt;

<!-- 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">之间切换teslateslawhite的src。我知道"Tesla"是一个相当愚蠢的例子。随意用您想要的内容进行更改。

英文:

It's pretty easy. That's just copy & paste & you're done.

const myBtn = document.getElementById(&quot;darktheme&quot;);
const img = document.getElementById(&quot;ferrarijs&quot;)
const img2 = document.getElementById(&quot;teslajs&quot;)


let mySrc = img.getAttribute(&quot;src&quot;);
let counter = 0;

myBtn.addEventListener(&quot;click&quot;, function() {
  if (counter % 2 === 0) {
    img.setAttribute(&quot;src&quot;, &quot;images/ferrari.jpg&quot;);
  } else {
    img.setAttribute(&quot;src&quot;, &quot;images/ferrariwhte.jpg&quot;);
  }
  if (counter % 2 === 0) {
    img2.setAttribute(&quot;src&quot;, &quot;images/tesla.jpg&quot;);
  } else {
    img2.setAttribute(&quot;src&quot;, &quot;images/teslawhte.jpg&quot;);
  }
  
  counter++;
});

This will toggle the src of &lt;img src=&quot;teslawhite.jpg&quot; src=&quot;teslajs&quot;&gt; between tesla & teslawhite. I know "Tesla" is a pretty dumb example. Feel free to change it with what you want.

答案3

得分: 0

你可以使用 querySelectorAll 来查找所有的 &lt;img&gt;

然后循环遍历它们以设置 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 &lt;img&gt;'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 = [
  &#39;https://placehold.co/150?text=Image-0&#39;,
  &#39;https://placehold.co/150?text=Image-1&#39;,
  &#39;https://placehold.co/150?text=Image-2&#39;,
  &#39;https://placehold.co/150?text=Image-3&#39;
];

const myBtn = document.getElementById(&quot;darktheme&quot;);
const imgs = [ ...document.querySelectorAll(&quot;img&quot;) ];

myBtn.addEventListener(&quot;click&quot;, function() {
    imgs.forEach(i =&gt; {
        let oldIndex = srcs.findIndex(e =&gt; e === i.src);
        let newIndex = (oldIndex === srcs.length - 1) ? 0 : oldIndex + 1;
        i.src = srcs[newIndex];
    });
});

<!-- language: lang-html -->

&lt;button id=&#39;darktheme&#39;&gt;Click me!&lt;/button&gt;

&lt;img src=&#39;https://placehold.co/150?text=Image-0&#39; /&gt;
&lt;img src=&#39;https://placehold.co/150?text=Image-0&#39; /&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月18日 00:24:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定