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

标签 javascript html function

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

相关文章:

javascript - PIXI.js 渲染不工作

javascript - Bootstrap 'select' 样式,使用jquery转化为bootstrap下拉菜单

html - 中间的 CSS 裁剪字符串

javascript - 文本选择 API 去哪儿了?

javascript - 鼠标悬停在节点文本下划线

javascript - 使用回调将两个数字相加和相乘

python - 1 作为函数声明中的参数类型

javascript - 你如何将属性发送到 svg 中的 javascript 函数

javascript - Icecast json-status.xls,歌曲标题为空白的无效 json 答案

mysql - 带有索引列的 WHERE 子句中的 "CASE WHEN"