gpt4 book ai didi

javascript - 在外部 JavaScript 表中使用多个函数

转载 作者:行者123 更新时间:2023-11-28 11:53:46 25 4
gpt4 key购买 nike

在我的“网站”中,我有一系列谜语,一旦您单击它们,就会显示答案。在外部 javascript 表中,我有十个单独的函数,如下所示:

function myQuestion1() {document.getElementById("answer1").innerHTML = "When it's turned into the teacher";}

我正在尝试弄清楚如何使用多重功能。我已经有一个关于此功能的测试问题。在 HTML 中我是这样写的:

<p onClick="revealAnswer('answer11','When it is turned into the teacher')">When is homework not homework?</p>


在我的外部 JavaScript 表中,我是这样写的:

function revealAnswer(answerId, answer){document.getElementById("answerId").innerHTML = "answer";}

我有什么遗漏吗?

最佳答案

不要引用你的变量,否则它们会被解释为字符串:

var x = "Hello there!";
console.log(x); //"Hello there!"
console.log("x"); //"x"

所以,对于你的代码:

function revealAnswer(answerId, answer) {
document.getElementById(answerId).innerHTML = answer;
}

关于javascript - 在外部 JavaScript 表中使用多个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com