所以,我是一个新手 javascript 编码员,只是刚接触过我的脚。我正在构建一个小型应用程序,它通过单击按钮从一组两个数组(一个名词数组和一个形容词数组)中各添加一个值来生成随机生成的运动队名称。到目前为止,我可以生成一个团队名称,但是再次单击时并没有替换旧的团队名称,而是保留了旧名称,我必须刷新页面才能让按钮加载新的团队名称。
我觉得答案比我想象的要简单得多,但我已经意识到这是期末考试周,我不能把所有时间都花在这上面。我需要帮助。
这是codepen上的相关代码…… http://codepen.io/bryvl/pen/bNpgLg
您应该注意,因为代码笔结果 Pane 中没有“刷新”,您可能只需要在 html 文本中的某处键入一个空格,以便 Pane 自行刷新。
此外,我是菜鸟,所以实际上可能有一种刷新 Pane 的方法…… 反正!我们将非常感谢您的帮助!提前致谢!最佳答案
移动 var randTeamName = ...
inside 您的 displayRandName()
函数:
function displayRandName() {
var randTeamName = "The" + " " + nameAdjective[Math.floor(Math.random() * nameAdjective.length)] + " " + nameNoun[Math.floor(Math.random() * nameNoun.length)];
//Just for me to see in the editor
console.log(randTeamName);
document.getElementById("output").innerHTML = randTeamName;
}
关于javascript - 尝试在不刷新页面的情况下单击按钮显示新的随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27437365/