因此,基本上,当我点击一个按钮时,我希望它使用两个数组中的单词随机生成一个应用程序想法。我已经被卡住好几天了,我只是不确定错误在哪里。当我试着只运行第二个函数和id时,它什么都没做,所以我认为问题出在函数或数组中。我把所有东西都命名对了吗?如果我在新中用错了任何词汇,请原谅我
<html>
<button onclick="randomizefunction(); randomizefunction2()">Give me an
app idea!</button>
<p id="randomWord"; "randomWord2"></p>
</html>
<script>
const myList = ["Calculator", "MOBA", "Fitness App", "Dating App"];
randomizeFunction()
function randomizeFunction() {
document.getElementById("randomWord").innerHTML = myList[Math.floor(Math.random() *
myList.length)]
}
const myList2 = ["with lootboxes", "with video ads every 15 seconds", "with pay to win
microstransactions"];
randomizeFunction2()
function randomizeFunction2() {
document.getElementById("randomWord2").innerHTML = myList2[Math.floor(Math.random() *
myList2.length)]
}
</script>
您可以在<p>
元素中使用<span>
标记(每个标记都有自己的ID(来执行您想要的操作。
请注意,只有一个元素可以使用ID名称(即ID名称必须唯一(和一个元素只能有一个ID。
类几乎与ID完全相同,但:(a(多个元素可以具有相同的className,(b(一个元素可以有多个className。
这就是为什么许多库(如Bootstrap(仅使用类的原因(ID做类不能做的事情的一个地方是HTML书签(。除此之外,只需对所有内容使用类即可。
此外,请注意您的按钮javascript:randomizefunction
!==
randomizeFunction
中有一个拼写错误。
以下是您的代码工作的证据:
const myList = ["Calculator", "MOBA", "Fitness App", "Dating App"];
const myList2 = ["with lootboxes", "with video ads every 15 seconds", "with pay to win microstransactions"];
randomizeFunction();
randomizeFunction2();
function randomizeFunction() {
document.getElementById("randomWord").innerHTML = myList[Math.floor(Math.random() *
myList.length)];
}
function randomizeFunction2() {
document.getElementById("randomWord2").innerHTML = myList2[Math.floor(Math.random() *
myList2.length)];
}
<button onclick="randomizeFunction(); randomizeFunction2()">Give me an
app idea!</button>
<p><span id="randomWord"></span> <span id="randomWord2"></span></p>
您的第二个函数不会运行,因为您不能向一个元素添加多个Id
如果删除第二个Id,则html代码如下所示:
<p id="randomWord"></p>
或者你可以使用类名:
<p class="randomWord randomWord2"></p>
如果使用一个Id,则不再需要第二个函数
如果要使用类名,则需要更改
document.getElementById(id)
至
document.querySelector(classname)
这样说吧,一个HTML元素只能有1个id
,就像一个员工在一个公司只能有一个付款代码一样,一个付款码不能用来为两个员工准备付款单,一旦第一个员工收到付款,该代码将无效,这意味着下一个员工将无薪:(
HTML就是这样,当一个id被分配给一个以上的元素时,DOM树中的第一个元素将是合法的所有者。
说到JavaScript,这种语言是CaseSensitive的,函数、变量等都必须以这种方式处理。
<html>
//<button onclick="randomizefunction(); randomizefunction2()">Give me an
app idea!</button>
//check the function names for case-matching
<button onclick="randomizeFunction(); randomizeFunction2()">Give me an
app idea!</button>
//<p id="randomWord"; "randomWord2"></p>
//two different elements for your purpose
<p id="randomWord"></p>
<p id="randomWord2"></p>
</html>
<script>
const myList = ["Calculator", "MOBA", "Fitness App", "Dating App"];
randomizeFunction()
function randomizeFunction() {
document.getElementById("randomWord").innerHTML = myList[Math.floor(Math.random()
* myList.length)]
}
const myList2 = ["with lootboxes", "with video ads every 15 seconds", "with pay to win
microstransactions"];
randomizeFunction2()
function randomizeFunction2() {
document.getElementById("randomWord2").innerHTML = myList2[Math.floor(Math.random() *
myList2.length)]
}
</script>
但在您自己的情况下,我想您想要实现的是在相同的<p>
元素中连接第一个函数和第二个函数的结果,要实现这一点,只需使用:
function randomizeFunction2() {
//note the use of the same `randomWord` (2)has been removed
//also note the `+=` for concatenation
document.getElementById("randomWord").innerHTML += myList2[Math.floor(Math.random() * myList2.length)]
}