[英]Get Random Image with Button Click with Javascript
I know this has been answered before, but I can't for the life of me figure out why this wouldn't work.我知道之前已经回答过这个问题,但我终其一生都无法弄清楚为什么这行不通。 I've checked, and re-checked, and everything looks like it should work outright, but I'm not sure what I would be missing with this.
我已经检查并重新检查,一切看起来都应该可以正常工作,但我不确定我会错过什么。 Here's what I have:
这是我所拥有的:
JS: JS:
const images = [
"https://assets.codepen.io/8689/lake-thumb.jpg",
"https://assets.codepen.io/8689/photo-thumb.jpg",
"https://assets.codepen.io/8689/gopro-thumb.jpg"
];
function getRandomImage() {
const rnd = Math.floor(Math.random() * images.length);
return images[rnd];
}
function changeImage() {
const img = document.querySelector("#images");
img.src = getRandomImage();
}
function init() {
const btn = document.querySelector(".btn-image");
btn.onclick = changeImage;
}
window.onload = init;
HTML: HTML:
<div id="images"></div>
<a href="#" class="btn-img">Get Image</a>
Codepen link: https://codepen.io/ultraloveninja/pen/ZEOLKeV Codepen 链接: https ://codepen.io/ultraloveninja/pen/ZEOLKeV
It should work and show a random image on load and then when you click the link/button load up a new on.它应该可以工作并在加载时显示随机图像,然后当您单击链接/按钮时加载一个新图像。 So, not sure why they wouldn't be showing overall and I'm not getting any errors in the console.
所以,不知道为什么他们不会整体显示,我在控制台中没有收到任何错误。
You need an <img>
instead of a <div>
element and the class name is wrong for the <a>
element here is a correction of your code您需要一个
<img>
而不是<div>
元素,并且<a>
元素的类名是错误的,这里是您的代码的更正
const images = [ "https://assets.codepen.io/8689/lake-thumb.jpg", "https://assets.codepen.io/8689/photo-thumb.jpg", "https://assets.codepen.io/8689/gopro-thumb.jpg" ]; function getRandomImage() { const rnd = Math.floor(Math.random() * images.length); return images[rnd]; } function changeImage() { const img = document.querySelector("#images"); img.src = getRandomImage(); } function init() { const btn = document.querySelector(".btn-img"); btn.onclick = changeImage; } window.onload = init;
<img id="images"> <a href="#" class="btn-img">Get Image</a>
Your <div id="images"></div>
tries to change its src
here:您的
<div id="images"></div>
尝试在此处更改其src
:
const img = document.querySelector("#images");
img.src = getRandomImage();
however it does not have an image source, since it's a div.但是它没有图像源,因为它是一个 div。
it should be: <img id="images">
instead of the div.它应该是:
<img id="images">
而不是 div。
You need to investigate the issue step by step:您需要逐步调查问题:
Make sure that init
is actually running (add a console.log in the beginning of it - see/run the code below) OK, init
works as you can see...确保
init
确实正在运行(在它的开头添加一个 console.log - 请参阅/运行下面的代码)好的, init
可以正常工作...
Make sure that you capture the button correctly (add a console.log with it) Oh, it's actually null
and then there is an error of adding an event listener to a null value确保你正确捕获按钮(用它添加一个console.log)哦,它实际上是
null
,然后将事件侦听器添加到空值时出现错误
Why is that?这是为什么? Oh, you have
btn-img
on the button, but btn-image
in your code.哦,按钮上有
btn-img
,但代码中有btn-image
。 Making both class names the same solves the problem.使两个类名相同可以解决问题。
The second problem you have is that the image should be <img id="images">
and not a div
您遇到的第二个问题是图像应该是
<img id="images">
而不是div
const images = [ "https://assets.codepen.io/8689/lake-thumb.jpg", "https://assets.codepen.io/8689/photo-thumb.jpg", "https://assets.codepen.io/8689/gopro-thumb.jpg" ]; function getRandomImage() { const rnd = Math.floor(Math.random() * images.length); return images[rnd]; } function changeImage() { const img = document.querySelector("#images"); img.src = getRandomImage(); } function init() { console.log("Init works!") // NEW! const btn = document.querySelector(".btn-image"); console.log("Button:", btn) // NEW! btn.onclick = changeImage; } window.onload = init;
<div id="images"></div> <a href="#" class="btn-img">Get Image</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.