Javascript 我正在尝试制作一个简单的程序,每次我点击它时,它都会将innerText更改为不同的单词

Javascript 我正在尝试制作一个简单的程序,每次我点击它时,它都会将innerText更改为不同的单词,javascript,Javascript,当我点击我的p元素时,它在html中设置了hi这个词,这个词会像我预期的那样从这个列表中变成一个随机词,但它只会做一次。我必须刷新页面并再次单击,以便在单击时再次更改。有人知道为什么吗?您的randomNum变量只设置了一次。因此,每次单击元素时,它都会将单词设置为相同的索引 通过将随机数生成器放入onClick中来修复此问题,如下所示: const word = document.getElementById("myText"); const randomWords =

当我点击我的p元素时,它在html中设置了hi这个词,这个词会像我预期的那样从这个列表中变成一个随机词,但它只会做一次。我必须刷新页面并再次单击,以便在单击时再次更改。有人知道为什么吗?

您的randomNum变量只设置了一次。因此,每次单击元素时,它都会将单词设置为相同的索引

通过将随机数生成器放入onClick中来修复此问题,如下所示:

const word = document.getElementById("myText");


const randomWords = ["gave","column","judge","hair","single","were",
"such","rather","area","board","black","fence",
"cost","matter","condition","that","slave","record",
"spring","ocean","society","general","central","page",
"successful","divide","community","men","century","did",
"discussion","past","bare","ever","in","brick",
"though","prove","film","unless","reach","complete"]

const randomNum = Math.floor(Math.random() * 40);

word.onclick = function() {
    this.innerText = randomWords[randomNum];
}
word.onclick = function() {
    var randomNum = Math.floor(Math.random() * 40)
    this.innerText = randomWords[randomNum]
}
这样,每次触发onClick时都会再次生成该数字

另外,我建议使用randomWords.length而不是40,这样您就可以在不破坏代码的情况下从数组中添加和删除单词,如下所示:

const word = document.getElementById("myText");


const randomWords = ["gave","column","judge","hair","single","were",
"such","rather","area","board","black","fence",
"cost","matter","condition","that","slave","record",
"spring","ocean","society","general","central","page",
"successful","divide","community","men","century","did",
"discussion","past","bare","ever","in","brick",
"though","prove","film","unless","reach","complete"]

const randomNum = Math.floor(Math.random() * 40);

word.onclick = function() {
    this.innerText = randomWords[randomNum];
}
word.onclick = function() {
    var randomNum = Math.floor(Math.random() * 40)
    this.innerText = randomWords[randomNum]
}
您的randomNum变量只设置一次。因此,每次单击元素时,它都会将单词设置为相同的索引

通过将随机数生成器放入onClick中来修复此问题,如下所示:

const word = document.getElementById("myText");


const randomWords = ["gave","column","judge","hair","single","were",
"such","rather","area","board","black","fence",
"cost","matter","condition","that","slave","record",
"spring","ocean","society","general","central","page",
"successful","divide","community","men","century","did",
"discussion","past","bare","ever","in","brick",
"though","prove","film","unless","reach","complete"]

const randomNum = Math.floor(Math.random() * 40);

word.onclick = function() {
    this.innerText = randomWords[randomNum];
}
word.onclick = function() {
    var randomNum = Math.floor(Math.random() * 40)
    this.innerText = randomWords[randomNum]
}
这样,每次触发onClick时都会再次生成该数字

另外,我建议使用randomWords.length而不是40,这样您就可以在不破坏代码的情况下从数组中添加和删除单词,如下所示:

const word = document.getElementById("myText");


const randomWords = ["gave","column","judge","hair","single","were",
"such","rather","area","board","black","fence",
"cost","matter","condition","that","slave","record",
"spring","ocean","society","general","central","page",
"successful","divide","community","men","century","did",
"discussion","past","bare","ever","in","brick",
"though","prove","film","unless","reach","complete"]

const randomNum = Math.floor(Math.random() * 40);

word.onclick = function() {
    this.innerText = randomWords[randomNum];
}
word.onclick = function() {
    var randomNum = Math.floor(Math.random() * 40)
    this.innerText = randomWords[randomNum]
}

每次都需要在click处理程序中选择一个新的randomNum。多次运行的代码就是函数中的代码。当前在页面最初加载时只生成一次随机数,而不是每次单击move const randomNum=Math.floorMath.random*40;在onclick函数内部注意:不要硬编码值40。使用随机词。长度。这使得从列表中添加/删除单词更加容易。每次都需要在单击处理程序中选择一个新的randomNum。多次运行的代码就是函数中的代码。当前在页面最初加载时只生成一次随机数,而不是每次单击move const randomNum=Math.floorMath.random*40;在onclick函数内部注意:不要硬编码值40。使用随机词。长度。这使得从列表中添加/删除单词更容易。