javascript - Javascript 不更新 randomElement()
问题描述
我试图在javascript中随机选择一个图像,它可以工作,但是当我点击一个按钮并且图像发生变化时,randomElement
仍然和第一次工作时一样。我正在尝试使用相同的名称更新它以进行另一次随机选举,但不起作用。这是代码:
document.addEventListener('DOMContentLoaded', function() {
const catbutton=document.getElementById('catbutton')
const cubebutton=document.getElementById('cubebutton')
const image=document.getElementById('image')
var x=0
var arr= ["pr1.jpg", "pr2.jpg"]
const randomElement = arr[Math.floor(Math.random() * arr.length)];
console.log(randomElement)
image.src=(randomElement);
catbutton.onclick=function() {catfunction()};
function catfunction(){
if (randomElement=="pr1.jpg"){
x++;
alert(x)
const randomElement = arr[Math.floor(Math.random() * arr.length)];
console.log(randomElement)
image.src=(randomElement);
}
else if (randomElement=="pr2.jpg"){
alert("Fallaste!")
}
}
cubebutton.onclick=function() {cubefunction()};
function cubefunction(){
if (randomElement=="pr2.jpg"){
x++;
alert(x)
const randomElement = arr[Math.floor(Math.random() * arr.length)];
console.log(randomElement)
image.src=(randomElement);
}
else if (randomElement=="pr1.jpg"){
alert("Fallaste!")
}
}
})
解决方案
在
if (randomElement=="pr1.jpg"){
x++;
alert(x)
const randomElement = arr[Math.floor(Math.random() * arr.length)];
console.log(randomElement)
image.src=(randomElement);
}
这
const randomElement = arr[Math.floor(Math.random() * arr.length)];
是一个const 声明,对其他同名声明没有影响。
有关块作用域的解释,请参阅“JavaScript:作用域简介(函数作用域、块作用域)”。
如果删除const
关键字,它将变为
if (randomElement=="pr1.jpg"){
x++;
alert(x)
randomElement = arr[Math.floor(Math.random() * arr.length)];
console.log(randomElement)
image.src=(randomElement);
}
分配给randomElement
此处声明的:
const randomElement = arr[Math.floor(Math.random() * arr.length)];
然后您会遇到问题,因为您只能分配一次const
声明。您应该将其更改为let
允许您在按钮处理程序运行时分配新值的声明。
document.addEventListener('DOMContentLoaded', function() {
const catbutton=document.getElementById('catbutton')
const cubebutton=document.getElementById('cubebutton')
const image=document.getElementById('image')
var x=0
var arr= ["pr1.jpg", "pr2.jpg"]
let randomElement = arr[Math.floor(Math.random() * arr.length)];
console.log(randomElement)
image.src=(randomElement);
catbutton.onclick=function() {catfunction()};
function catfunction(){
if (randomElement=="pr1.jpg"){
x++;
alert(x)
randomElement = arr[Math.floor(Math.random() * arr.length)];
console.log(randomElement)
image.src=(randomElement);
}
else if (randomElement=="pr2.jpg"){
alert("Fallaste!")
}
}
cubebutton.onclick=function() {cubefunction()};
function cubefunction(){
if (randomElement=="pr2.jpg"){
x++;
alert(x)
randomElement = arr[Math.floor(Math.random() * arr.length)];
console.log(randomElement)
image.src=(randomElement);
}
else if (randomElement=="pr1.jpg"){
alert("Fallaste!")
}
}
})
推荐阅读
- bash - 使用 Curl 数据二进制选项,内存不足
- android - 使用文件浏览器对话窗口访问资产文件夹文件
- typescript - 一种更清洁的方式,如何重构异步调用
- vue.js - 如何在动态加载的数据下动态地在 Vue.js 中进行计算
- python-2.7 - 如何从 purchase.order 中获取产品描述的值
- swift - swift - 在变量名中使用整数 for 循环
- amazon-ec2 - www 和 http 在 AWS 中有效,但不是 https
- selenium - 更改 Mozilla 只读属性
- python - 仿射变换后不同高度的字母
- php - 我正在尝试将字符串转换为 php 中的数组,因为我有下面给出的字符串