[英]Javascript Random image with text on click button
I've been following some other tutorials and managed to get an image combined with specific text to display on load and a different image and combined text after refresh. 我一直在关注其他一些教程,并设法使图像与特定文本组合以在加载时显示,并在刷新后显示不同的图像和组合文本。
But now i want to connect it to a button so the random combination of text and image only show on click, the button doesn't do anything. 但是现在我想将其连接到按钮,以便仅在单击时显示文本和图像的随机组合,该按钮没有任何作用。
Javascript in the head tags 标头中的Javascript
<div id="quote"></div> <input class="Randombutton" style="float: left;" type="button" value="Randomize" onclick="randomImg()"> <script> (function randomImg() { var quotes = [ { text: "BIO1", img: "images/captainamerica.jpg" }, { text: "BIO2", img: "images/hulk.jpg", }, { text: "BIO3", img: "images/spiderman.jpg", }, { text: "BIO4", img: "images/blackwidow.jpg", }, { text: "BIO5", img: "images/ironman.png", }, ]; var quote = quotes[Math.floor(Math.random() * quotes.length)]; document.getElementById("quote").innerHTML = '<p>' + quote.text + '</p>' + '<img src="' + quote.img + '">'; })(); </script>
Any help is very much appreciated. 很感谢任何形式的帮助。 Thank you.
谢谢。
Does this meet what you need? 这符合您的需求吗?
function randomImg() { var quotes = [ { text: "BIO1", img: "images/captainamerica.jpg" }, { text: "BIO2", img: "images/hulk.jpg", }, { text: "BIO3", img: "images/spiderman.jpg", }, { text: "BIO4", img: "images/blackwidow.jpg", }, { text: "BIO5", img: "images/ironman.png", }, ]; var quote = quotes[Math.floor(Math.random() * quotes.length)]; document.getElementById("quote").innerHTML = '<p>' + quote.text + '</p>' + '<img src="' + quote.img + '">'; }
<div id="quote"></div> <input class="Randombutton" style="float: left;" type="button" value="Randomize" onclick="randomImg()">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.