如何更改这个JavaScript或复制它,以使两个按钮和图像都具有两个点击功能?

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

how could i change this java script or copy it to have two on click function for two buttons and images

问题

html:

<div class="vaultcontainer">
  <img src="img/vault.svg" alt="" id="image">
</div>

<button id="image-button">click </button>
<button id="image-button2">click </button>

js:

const button = document.getElementById('image-button');
const image = document.getElementById('image');

button.addEventListener('click', function() {
  changeImage();
});

function changeImage() {
  image.src = 'img/vault2.svg';
}
英文:

html:

&lt;div class=&quot;vaultcontainer&quot;&gt;
  &lt;img src=&quot;img/vault.svg&quot; alt=&quot;&quot; id=&quot;image&quot;&gt;

&lt;/div&gt;

&lt;button id=&quot;image-button&quot;&gt;click &lt;/button&gt;
&lt;button id=&quot;image-button2&quot;&gt;click &lt;/button&gt;

js:

const button = document.getElementById(&#39;image-button&#39;);
const image = document.getElementById(&#39;image&#39;);

button.addEventListener(&#39;click&#39;, function() {
  changeImage();
});

function changeImage() {
  image.src = &#39;img/vault2.svg&#39;;
}

答案1

得分: 1

Sure, here's the translated content:

使用一个类:

&lt;button class=&quot;js-changeImage&quot;&gt;点击 &lt;/button&gt;
&lt;button class=&quot;js-changeImage&quot;&gt;点击 &lt;/button&gt;

然后,在JS中使用 querySelectorAll() 和返回的NodeList上的 .forEach()

const buttons = document.querySelectorAll(&#39;js-changeImage&#39;);

buttons.forEach(btn =&gt; btn.addEventListener(&#39;click&#39;, changeImage));

如果您需要为按钮指定不同的图像,请使用 data-* 属性

&lt;img src=&quot;img/vault.svg&quot; alt=&quot;&quot; id=&quot;image&quot;&gt;

&lt;button data-image=&quot;img/cat.svg&quot;&gt;点击 &lt;/button&gt;
&lt;button data-image=&quot;img/dog.svg&quot;&gt;点击 &lt;/button&gt;
const image = document.querySelector(&#39;#image&#39;);
const buttons = document.querySelectorAll(&#39;[data-image]&#39;);

buttons.forEach(btn =&gt; btn.addEventListener(&#39;click&#39;, () =&gt; {
  changeImage(btn.dataset.image);
});

function changeImage(src) {
  image.src = src;
}
英文:

Use a class:

&lt;button class=&quot;js-changeImage&quot;&gt;click &lt;/button&gt;
&lt;button class=&quot;js-changeImage&quot;&gt;click &lt;/button&gt;

then, in JS use querySelectorAll() and .forEach() on the returned NodeList:

const buttons = document.querySelectorAll(&#39;js-changeImage&#39;);

buttons.forEach(btn =&gt; btn.addEventListener(&#39;click&#39;, changeImage));

If you need to specify different images for your buttons — use the data-* attribute:

&lt;img src=&quot;img/vault.svg&quot; alt=&quot;&quot; id=&quot;image&quot;&gt;

&lt;button data-image=&quot;img/cat.svg&quot;&gt;click &lt;/button&gt;
&lt;button data-image=&quot;img/dog.svg&quot;&gt;click &lt;/button&gt;
const image = document.querySelector(&#39;#image&#39;);
const buttons = document.querySelectorAll(&#39;[data-image]&#39;);

buttons.forEach(btn =&gt; btn.addEventListener(&#39;click&#39;, () =&gt; {
  changeImage(btn.dataset.image);
});

function changeImage(src) {
  image.src = src;
}

huangapple
  • 本文由 发表于 2023年5月14日 20:35:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247512.html
匿名

发表评论

匿名网友

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

确定