javascript 链接在点击时不起作用;页面加载时功能有效

标签 javascript function random shuffle

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/

相关文章:

php - 如何通过php过滤任意给定数量的html标签

jquery - 使用 jquery 随机淡入和淡出

javascript - VSCode如何禁用/删除 "react/cjs/react.development"自动完成

python - 中断外部循环 Python 函数其他选项

python - 在 Python 中设置函数签名

c - ispalindrome C,检查空间的问题

javascript - Discord.js v12.2.0 |根据 react 添加角色

javascript - 离开编辑后的 ​​html 表单前提示确认

javascript - 如果我使用扩展语法,如何才能完全返回内部包含嵌套对象数组的数组?

c - 在 C 中生成离散均匀分布