我试图通过点击超链接生成随机图像。为此,我使用随机函数生成随机图像。但它不起作用。任何人都可以纠正我吗?
代码:
<!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>
答案 0 :(得分:0)
link.addEventListener("click",function() {
var randomNum = Math.floor(Math.random() * images.length);
console.log('link : ',images[randomNum]);
});
&#13;