为什么我的作业代码中出现“未定义”错误?

时间:2017-03-21 22:55:31

标签: javascript

在下面的家庭作业中,我已经完成了从章节中我能想到的一切,但它根本不起作用我觉得我错过了什么或者我搞砸了某个地方因为当我将鼠标悬停在上面时光标也不会改变嵌套列表。

其中一条指示说:

  

“使用经历mousedown事件的列表项的id属性,使用parseInt()函数提取索引号”

但似乎我所做的并不正确。

window.onload = setup; 

function setup() { 
    var questions = document.querySelectorAll('ol li'); 

    for (var i = 0; i < questions.length; i++) { 
        questions[i].id = i + "phrase"; 
        questions[i].onmousedown = showEnglish(questions[i]);
        questions[i].onmouseup = showFrench(questions[i]); 
        questions[i].style.cursor = "pointer";
    }
}

function showEnglish() { 
    var phraseNumber = parseInt(questions[i].id); 

    phraseNumber.innerHTML = english[phraseNumber]; 
    phraseNumber.style.font = "italic"; 
    phraseNumber.style.color = "rgb(191, 22, 31)";
}

function showFrench() { 
    var phraseNumber = parseInt(questions[i].id); 

    phraseNumber.innerHTML = french[phraseNumber]; 
    phraseNumber.style.fontStyle = ""; 
    phraseNumber.style.color = "";
}

我收到错误消息,指出questionsquestions[i].onmousedown = showEnglish(questions[i]);未定义var phraseNumber = parseInt(questions[i].id); <ol> <li>Cet h&#244;tel n'est pas loin de la Tour Eiffel.</li> <li>A quelle heure arrive le train?</li> <li>Nous attendons l'autobus depuis une demi-heure.</li> <li>Ce repas est d&#233;licieux.</li> <li>Quel jour va-t-elle arriver?</li> <li>Nous avons onze minutes avant le d&#233;part du train!</li> <li>Habiter dans un pays &#233;tranger est une bonne exp&#233;rience.</li> <li>Excusez-moi! Je suis en retard!</li> <li>Est-ce que ce taxi est libre?</li> <li>Faites attention quand vous descendez l'escalier.</li> </ol>

我一直在用Notepad ++编写代码如果重要的话。

Html片段:

package Grading;

import java.util.Scanner;


public class Grading {


    public static void main(String[] args) {


    Scanner input =new Scanner(System.in);
    public static double quiz1, quiz2, quiz3, midterm, finalExam, Grades, totalGrade, bothQuizzes, PfinalExam, PmidTerm;
    public static String studentname;
    public static int Score;


    public String getstudentname( )
    {
        return studentname;
    }
    public double getquiz1()
    {
        return quiz1;
    }

    public double getquiz2 ()
    {
        return quiz2;
    }
public double getquiz3 ()
    {
        return quiz3;
    }
    public double midterm()
    {
        return midterm;
    }

    public double finalExam()
    {
        return finalExam;
    }

    public void setquiz1 (double quiz1)
    {
        this.quiz1 = quiz1;
    }

    public void setquiz2 (double quiz2)
    {
        this.quiz2 = quiz2;
    }
    public void setquiz3 (double quiz3)
    {
        this.quiz3 = quiz3;
    }

    public void setmidterm()
    {
        this.midterm = midterm;
    }

    public void setfinalExam()
    {
        this.finalExam = finalExam;
    }

    public void setGrades ()
    {

    }

    public String toString(){

        return this.quiz1 + " " + this.quiz2 + this.quiz3 + " " + this.midterm + " " + this.finalExam;

    }



    public static void  readInput(){

        System.out.println("Please enter the grade you got for the first quiz: ");
        quiz1 = grades.nextInt();

        while (quiz1 <0 || quiz1>10)
        {
            System.out.println("Please enter a grade between zero and ten: ");
            quiz1 = grades.nextInt();
        }

        System.out.println("Please enter the grade you got for the second quiz: ");
        quiz2 = grades.nextInt();

        while (quiz2 <0 || quiz2>10)

        {
            System.out.println("Please enter a grade between zero and ten: ");
            quiz2 = grades.nextInt();
        }

        System.out.println("Please enter the grade you got for the third quiz: ");
        quiz3 = grades.nextInt();

        while (quiz3 <0 || quiz3>10)

        {
            System.out.println("Please enter a grade between zero and ten: ");
            quiz3 = grades.nextInt();
        }

        System.out.println("Please enter the grade you got on your midterm: ");
        midterm = grades.nextInt();

        while (midterm <0 || midterm>100)

        {
            System.out.println("Please enter a grade between 0 and 100: ");
            midterm = grades.nextInt();
        }

        System.out.println("Please enter the grade you got on your final exam: ");
        finalExam = grades.nextInt();

        while (finalExam < 0 || finalExam > 100)

        {
            System.out.println("Please enter a grade between 0 and 100: ");
            finalExam = grades.nextInt();
        }
    }

    public static  void output()

    {

        System.out.println(" your score for the first quiz was " + quiz1 );

        System.out.println("your score for the second quiz was " + quiz2);

        System.out.println("your score for the third quiz was " + quiz3);

        System.out.println(" your score for the midterm was " + midterm );

        System.out.println("your score for the final exam was " + finalExam);
        bothQuizzes = ((quiz1 + quiz2 + quiz3)/100)*.25;
        PmidTerm = (midterm/100) *.35;
        PfinalExam = (finalExam/100) * .40;

        System.out.println("Your total grade for these grades is " + totalGrade + "%");

        System.out.println("Your total grade for these grades is " + totalGrade);

        double letterGrade = totalGrade;
        if (letterGrade >= 90)

        {

            System.out.println("Your grade is an A");

            // grade = "A";

        }

        else if (letterGrade >= 80)

        {

            System.out.println("Your grade is a B");

        }

        else if (letterGrade >= 70)

        {

            System.out.println("Your grade is a C");

        }

        else if (letterGrade >= 60)

        {

            System.out.println("Your grade is a D");

        }

        else

        {

            System.out.println("Your grade is an F");



        }

    }}

1 个答案:

答案 0 :(得分:1)

这不是您正确分配事件处理程序的方式:

for (var i = 0; i < questions.length; i++) {
    ....
    questions[i].onmousedown = showEnglish(questions[i]);
    ...
}

而是将其更改为:

questions[i].onmousedown = showEnglish;

您将该函数指定为事件处理程序,而不是在该特定时刻调用它。请注意,当您添加括号fn()时,您正在调用函数。

现在您必须更新showEnglish()功能以反映这一点。

function showEnglish() { 
    var phraseNumber = parseInt(this.id); // Here
    ...
}

请注意,我使用this关键字来引用该元素。

同样的过程适用于onmouseupshowFrench功能