var randomNumber
// generate the random numbers based on the length of the image array
randomNumber = Math.floor(Math.random() * topImages.length);
function shuffleTop(){
document.getElementById("topImage").src = topImages[randomNumber];
};
$("p a#shuffle").click(shuffleTop());
这是我源代码中的链接:
<p><a href="#" id="shuffle">SHUFFLE</a></p>
最佳答案
您正在运行该函数,它返回的内容正在分配给 cliek。您没有分配对函数 shuffleTop
的引用。
$("p a#shuffle").click(shuffleTop());
需要
$("p a#shuffle").click(shuffleTop);
并且您只生成一次数字,将随机数生成器移到里面。
function shuffleTop(){
randomNumber = Math.floor(Math.random() * topImages.length);
document.getElementById("topImage").src = topImages[randomNumber];
};
关于javascript 链接在点击时不起作用;页面加载时功能有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29496891/