在我的“网站”中,我有一系列谜语,一旦您单击它们,就会显示答案。在外部 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/