尝试创建一个不可见的按钮来更改一张图片。

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

Trying to create an invisible button to change an image

问题

我正在尝试在页面的一个小部分上放置一个不可见按钮以更改背景图片。

但是,按钮没有更改为指定的图像

var image = document.getElementById('myImage');

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

function changeImage(){
  image.src = 'img/lab.svg';
}
<img src="img/vault.svg" alt="">
<button id="myImage"> </button>
英文:

I am trying to have an invisible button on a small part of the page to change the background image.

But, the button doesn't change the image to the one specified.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var image = document.getElementById(&#39;myImage&#39;);

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

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

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

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

<!-- end snippet -->

答案1

得分: 2

你似乎将按钮引用(变量 image)与 &lt;img&gt; 元素混淆了。

请确保使用明智的名称(对于您的变量和 ID)以避免混淆。


你将按钮引用保存在变量 image 中,并尝试更改其 src 属性。

相反,你应该分配给图片的 src 属性:

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

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

function changeImage(){
  image.src = 'img/lab.svg';
}
<img id="image" src="img/vault.svg" alt="">
<button id="image-button"> </button>

注意:我更改了变量名和元素ID。

英文:

You seem to confuse your button reference (variable image) with the &lt;img&gt; element.

Make sure you use sensible names (for your variables and IDs) to avoid confusions.


You save the button reference in the variable image, and try to change its src property.

Instead, you should assign to the image's src attribute:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-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/lab.svg&#39;;
}

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

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

<!-- end snippet -->

Note: I changed the variable names and element IDs.

答案2

得分: 0

你可以使用image.onloadimage.onerror来了解发生了什么。

var image = document.getElementById('myImage');

image.addEventListener('click', function(){
    changeImage();
});
image.onerror = function(e) { console.log('加载失败', e); }
image.onload= function(e) { console.log('加载成功', e); }

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

You could use image.onload and image.onerror to understand what's happening.

var image = document.getElementById(&#39;myImage&#39;);

image.addEventListener(&#39;click&#39;, function(){
    changeImage();
});
image.onerror = function(e) { console.log(&#39;Loading failed&#39;, e); }
image.onload= function(e) { console.log(&#39;Loading successful&#39;, e); }

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

huangapple
  • 本文由 发表于 2023年5月6日 18:52:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188469.html
匿名

发表评论

匿名网友

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

确定