0

这怎么会返回错误?在我的头标签...

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()。怎么会?

谢谢。

4

3 回答 3

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&#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() 时传递,而不是在定义时传递

于 2012-11-17T17:49:18.213 回答
2

每个人是如何错过this.value论据列表中的?它应该是一个标识符,例如myvalue. 这就是为什么checkAnswer没有定义的原因。

现在,至于其余代码,您不能thischeckAnswer函数中使用,因为它将引用全局对象,而不是链接。此外,链接不支持该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属性,因为以数字开头的 ID 在 HTML5 中无效(即使这样,由于旧浏览器仍在广泛使用,它们也不是一个好主意,即使没有它们也毫无意义)

于 2012-11-17T17:53:24.943 回答
0

函数语法错误。

错误 :

function checkAnswer(this.value) {

函数语法必须是functionName(formalParameterName). 您在函数参数中传递对象中的值,这将导致错误。您必须将其更改为function checkAnswer(param).

于 2012-11-17T17:54:42.110 回答