gpt4 book ai didi

javascript - 为什么我的函数没有定义?

转载 作者:行者123 更新时间:2023-11-28 13:47:11 24 4
gpt4 key购买 nike

为什么会返回错误?在我的头部标签中...

var movieArray = [
["Everything is Illuminated", "0", ""],
["The Girl Who Leapt Through Time (Toki wo kakeru shojo)", "1", "<ol><li><span class=\"bold quote_actor\">Kosuke Yoshiyama: </span><span class=\"line\">It&#039;s not that rare. Many girls do it at your age.</span></li> </ol>"],
["Freedom Writers", "0", ""],
["Inside Man", "0", ""]
];

function checkAnswer(this.value) {
if (this.value == 0) {
alert ("Wrong answer!");
} else {
alert ("Correct Answer!");
}

}

在体内...

<p><a id="0" class="btn btn-primary btn-large" value="0" onclick="checkAnswer(this.value)">Everything is Illuminated</a></p>
<p><a id="1" class="btn btn-primary btn-large" value="1" onclick="checkAnswer(this.value)">The Girl Who Leapt Through Time (Toki wo kakeru shojo)</a></p>

错误是 checkAnswer() 未定义。怎么会这样?

谢谢。

最佳答案

您忘记通过添加反斜杠来转义数组元素内的"

var movieArray = [
["Everything is Illuminated", "0", ""],
["The Girl Who Leapt Through Time (Toki wo kakeru shojo)", "1", "<ol><li><span class=\"bold quote_actor\">Kosuke Yoshiyama: </span><span class=\"line\">It&#039;s not that rare. Many girls do it at your age.</span></li> </ol>"],
["Freedom Writers", "0", ""],
["Inside Man", "0", ""]
];

function checkAnswer(arg1) {
if (arg1 == 0) {
alert("Wrong answer!");
} else {
alert("Correct Answer!");
}

}​

其次,this.value 应该在调用 checkAnswer() 时传递,而不是在定义时传递

关于javascript - 为什么我的函数没有定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433095/

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