Javascript:点击链接后生成随机图像

时间:2017-10-21 01:54:24

标签: javascript

我试图通过点击超链接生成随机图像。为此,我使用随机函数生成随机图像。但它不起作用。任何人都可以纠正我吗?

代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="#" id="aclick"><p style="text-align: center;"> Catalogue </p> </a>


    <script language="JavaScript">
// set image list
var images = new Array();
images[0] = "final_images/book.jpg";
images[1] = "final_images/neck.jpeg";
images[2] = "final_images/shoes.jpg";
images[3] = "final_images/mixture.jpeg";
images[4] = "final_images/earing.jpg";

var link = document.getElementById('aclick');
link.addEventListener("click",function() {
    var randomNum = Math.floor(Math.random() * images.length);
});

</script>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
link.addEventListener("click",function() {
    var randomNum = Math.floor(Math.random() * images.length);
    console.log('link : ',images[randomNum]);
});
&#13;
&#13;
&#13;