无法使用JavaScript打印表

时间:2013-07-24 17:15:44

标签: javascript html dom

我想使用javascript以表格的形式打印对象的数据。我写的代码没有显示任何内容。请告诉我这里我做错了什么。

这是我写的代码 -

function Movie(id,movieName)
{
    this.id = id;
    this.movieName = movieName;
}

function MoviesWatched()
{
    this.movies = new Array();
}

MoviesWatched.prototype.addMovie = function(id,name)
{
    this.movies[id]=new Movie(id,name);
}
MoviesWatched.prototype.getTable = function()
{
    var movie;
    var table="<table border=1>";
    for(movie in this.movies)
    {
        table += "<tr><td>";
        table += movies[movie].id;
        table += "</td><td>";
        table += movies[movie].name;
        table += "</td></tr>";
    }
table += "</table>";
return table;
}

var myList=new MoviesWatched();

myList.addMovie(1,"Inception");
myList.addMovie(2,"Red");

document.write(myList.getTable());

1 个答案:

答案 0 :(得分:0)

在您的getTable函数中,您需要将movies更改为this.movies并将name更改为movieName

var movie;
var table="<table border=1>";
for(movie in this.movies)
{
    table += "<tr><td>";
    table += this.movies[movie].id;
    table += "</td><td>";
    table += this.movies[movie].movieName;
    table += "</td></tr>";
}

http://jsfiddle.net/fenderistic/9Gzdv/1/