为什么会返回错误? 在我头上的标签......
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'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()未定义。怎么样?
感谢。
答案 0 :(得分:3)
您忘记通过添加反斜杠
来逃避数组元素中的"
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'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()时传递,而不是在defitinion时传递
答案 1 :(得分:2)
每个人如何错过参数列表中的this.value
?它应该是标识符,例如myvalue
。这就是未定义checkAnswer
的原因。
现在,对于其余代码,您不能在this
函数中使用checkAnswer
,因为它将引用全局对象,而不是链接。此外,链接不支持value
属性,因此您需要使用getAttribute
或仅将值放在onClick
事件中。
function checkAnswer(myval) {
if (myval == 0) {
alert ("Wrong answer!");
} else {
alert ("Correct Answer!");
}
}
<p><a class="btn btn-primary btn-large" onclick="checkAnswer(0)">Everything is Illuminated</a></p>
<p><a class="btn btn-primary btn-large" onclick="checkAnswer(1)">The Girl Who Leapt Through Time (Toki wo kakeru shojo)</a></p>
我还删除了id
属性,因为除了HTML5之外,以数字开头的ID无效(即使那时它们也不是一个好主意,因为旧的浏览器仍然广泛使用,甚至没有他们没有意义)
答案 2 :(得分:0)
函数语法错误。
错误:
function checkAnswer(this.value) {
函数语法必须是functionName(formalParameterName)
。您正在函数参数中传递对象中的值,这将导致错误。您必须将其更改为function checkAnswer(param)
。