Javascript 将对象从数组添加到表
我一直在尝试,如何将我的学生对象添加到我的数组中。一旦解决了这个问题,我就试图在一个html文件中的表上显示数组中的内容 学生对象:Javascript 将对象从数组添加到表,javascript,arrays,Javascript,Arrays,我一直在尝试,如何将我的学生对象添加到我的数组中。一旦解决了这个问题,我就试图在一个html文件中的表上显示数组中的内容 学生对象: var Student = function (fullName, email, phone, category, groupID) { this.fullName = fullName; this.email = email; this.phone = phone; this.category = category; th
var Student = function (fullName, email, phone, category, groupID) {
this.fullName = fullName;
this.email = email;
this.phone = phone;
this.category = category;
this.groupID = groupID;
};
var studentArray = new Array(Student);
function makeTable() {
var student1 = new Student("Waw","waaw","awaw","waaw","waaw");
studentArray.push(student1);
document.write("<table>");
document.write("<thead><tr><th>Full Name</th><th>Email</th><th>Phone</th><th>Category</th><th>Group</th></tr></thead>");
document.write("<tbody>");
for(i = 0; i < studentArray.length; i++){
document.write("<tr><td>" + studentArray[i].fullName +"</td><td>" + studentArray[i].email +"</td><td>" + studentArray[i].phone +"</td><td>" + studentArray[i].category +"</td><td>" + studentArray[i].groupID +"</td></tr>");
}
document.write("</tbody>");
document.write("</table>");
}
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../script/scripts.js" type="text/javascript"></script>
</head>
<body>
<script>
makeTable();
</script>
</body>
</html>
学生阵列:
var Student = function (fullName, email, phone, category, groupID) {
this.fullName = fullName;
this.email = email;
this.phone = phone;
this.category = category;
this.groupID = groupID;
};
var studentArray = new Array(Student);
function makeTable() {
var student1 = new Student("Waw","waaw","awaw","waaw","waaw");
studentArray.push(student1);
document.write("<table>");
document.write("<thead><tr><th>Full Name</th><th>Email</th><th>Phone</th><th>Category</th><th>Group</th></tr></thead>");
document.write("<tbody>");
for(i = 0; i < studentArray.length; i++){
document.write("<tr><td>" + studentArray[i].fullName +"</td><td>" + studentArray[i].email +"</td><td>" + studentArray[i].phone +"</td><td>" + studentArray[i].category +"</td><td>" + studentArray[i].groupID +"</td></tr>");
}
document.write("</tbody>");
document.write("</table>");
}
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../script/scripts.js" type="text/javascript"></script>
</head>
<body>
<script>
makeTable();
</script>
</body>
</html>
makeTable函数:
var Student = function (fullName, email, phone, category, groupID) {
this.fullName = fullName;
this.email = email;
this.phone = phone;
this.category = category;
this.groupID = groupID;
};
var studentArray = new Array(Student);
function makeTable() {
var student1 = new Student("Waw","waaw","awaw","waaw","waaw");
studentArray.push(student1);
document.write("<table>");
document.write("<thead><tr><th>Full Name</th><th>Email</th><th>Phone</th><th>Category</th><th>Group</th></tr></thead>");
document.write("<tbody>");
for(i = 0; i < studentArray.length; i++){
document.write("<tr><td>" + studentArray[i].fullName +"</td><td>" + studentArray[i].email +"</td><td>" + studentArray[i].phone +"</td><td>" + studentArray[i].category +"</td><td>" + studentArray[i].groupID +"</td></tr>");
}
document.write("</tbody>");
document.write("</table>");
}
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../script/scripts.js" type="text/javascript"></script>
</head>
<body>
<script>
makeTable();
</script>
</body>
</html>
函数makeTable(){
var student1=新生(“Waw”、“waaw”、“awaw”、“waaw”、“waaw”);
学生阵列。推送(学生1);
文件。填写(“”);
编写(“全名EmailPhoneCategoryGroup”);
文件。填写(“”);
对于(i=0;i
Html文件:
var Student = function (fullName, email, phone, category, groupID) {
this.fullName = fullName;
this.email = email;
this.phone = phone;
this.category = category;
this.groupID = groupID;
};
var studentArray = new Array(Student);
function makeTable() {
var student1 = new Student("Waw","waaw","awaw","waaw","waaw");
studentArray.push(student1);
document.write("<table>");
document.write("<thead><tr><th>Full Name</th><th>Email</th><th>Phone</th><th>Category</th><th>Group</th></tr></thead>");
document.write("<tbody>");
for(i = 0; i < studentArray.length; i++){
document.write("<tr><td>" + studentArray[i].fullName +"</td><td>" + studentArray[i].email +"</td><td>" + studentArray[i].phone +"</td><td>" + studentArray[i].category +"</td><td>" + studentArray[i].groupID +"</td></tr>");
}
document.write("</tbody>");
document.write("</table>");
}
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../script/scripts.js" type="text/javascript"></script>
</head>
<body>
<script>
makeTable();
</script>
</body>
</html>
提供头衔
makeTable();
输出:
var Student = function (fullName, email, phone, category, groupID) {
this.fullName = fullName;
this.email = email;
this.phone = phone;
this.category = category;
this.groupID = groupID;
};
var studentArray = new Array(Student);
function makeTable() {
var student1 = new Student("Waw","waaw","awaw","waaw","waaw");
studentArray.push(student1);
document.write("<table>");
document.write("<thead><tr><th>Full Name</th><th>Email</th><th>Phone</th><th>Category</th><th>Group</th></tr></thead>");
document.write("<tbody>");
for(i = 0; i < studentArray.length; i++){
document.write("<tr><td>" + studentArray[i].fullName +"</td><td>" + studentArray[i].email +"</td><td>" + studentArray[i].phone +"</td><td>" + studentArray[i].category +"</td><td>" + studentArray[i].groupID +"</td></tr>");
}
document.write("</tbody>");
document.write("</table>");
}
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../script/scripts.js" type="text/javascript"></script>
</head>
<body>
<script>
makeTable();
</script>
</body>
</html>
我通常用Java编程,但是,我能想象这不起作用的唯一原因是,在将student1插入studentArray后,我的数组是错误的,或者我的数组仍然是空的
提前感谢。javascript是一种弱类型语言() 在这一行中,您不是将“Stundent”定义为数组的类型,而是将名为Student(在您的上下文中不存在)的对象推入数组“studentArray”。也就是说你把未定义的或空的数据放到了一个数组中
var studentArray = new Array(Student);
如果将其更改为此,则将创建一个空数组
var studentArray = new Array();
然后它应该会起作用
因为编译器不知道指向null的对象上的属性,所以某些浏览器会在下一行抛出异常
document.write("<tr><td>" + studentArray[i].fullName +"</td><td>" + studentArray[i].email +"</td><td>" + studentArray[i].phone +"</td><td>" + studentArray[i].category +"</td><td>" + studentArray[i].groupID +"</td></tr>");
document.write(“+studentArray[i]。全名+”+studentArray[i]。电子邮件+”+studentArray[i]。电话+”+studentArray[i]。类别+”+studentArray[i]。组ID+);
javascript是一种弱类型语言()
在这一行中,您不是将“Stundent”定义为数组的类型,而是将名为Student(在您的上下文中不存在)的对象推入数组“studentArray”。也就是说你把未定义的或空的数据放到了一个数组中
var studentArray = new Array(Student);
如果将其更改为此,则将创建一个空数组
var studentArray = new Array();
然后它应该会起作用
因为编译器不知道指向null的对象上的属性,所以某些浏览器会在下一行抛出异常
document.write("<tr><td>" + studentArray[i].fullName +"</td><td>" + studentArray[i].email +"</td><td>" + studentArray[i].phone +"</td><td>" + studentArray[i].category +"</td><td>" + studentArray[i].groupID +"</td></tr>");
document.write(“+studentArray[i]。全名+”+studentArray[i]。电子邮件+”+studentArray[i]。电话+”+studentArray[i]。类别+”+studentArray[i]。组ID+);
如果将Student
函数也推入数组,则无需执行此操作。它将返回未定义的值
简单地替换
var studentArray=新数组(Student)代码>
与
var studentArray=new Array()
如果将Student
函数也推入数组,则无需执行此操作。它将返回未定义的值
简单地替换
var studentArray=新数组(Student)代码>
与
var studentArray=new Array()代码>哇。。。我很抱歉。我想,就像在java中一样,我必须让数组知道它应该保存哪种类型。。。非常感谢你,兄弟。这并不能解决可视化的问题,一张空桌子<代码>新数组(学生)
仍将生成新数组。它将从单个元素开始,即Student
函数。它不会阻止循环显示在makeTable()
中生成的实际Student
实例。这表明它可以按原样工作,他们的代码中还有其他错误,因此可能是HTML中的某个输入错误?studentArray[i]
不会为空/未定义,因为他们的循环基于数组的长度,除非使用非常旧的浏览器,否则只会为fullName
等属性获取一个undefined
值,但即使在IE6模式下进行测试,它也会运行。如果它真的被修复了,那可能是一个js语法错误,在编辑时被忽略了,可能被修复了。。或者如果没有失败的话,会打印一行“未定义”的,为那些老浏览者执行。。。我很抱歉。我想,就像在java中一样,我必须让数组知道它应该保存哪种类型。。。非常感谢你,兄弟。这并不能解决可视化的问题,一张空桌子<代码>新数组(学生)
仍将生成新数组。它将从单个元素开始,即Student
函数。它不会阻止循环显示在makeTable()
中生成的实际Student
实例。这表明它可以按原样工作,他们的代码中还有其他错误,因此可能是HTML中的某个输入错误?studentArray[i]
不会为空/未定义,因为他们的循环基于数组的长度,除非使用非常旧的浏览器,否则只会为fullName
等属性获取一个undefined
值,但即使在IE6模式下进行测试,它也会运行。如果它真的被修复了,那可能是一个js语法错误,在编辑时被忽略了,可能被修复了。。或者,如果没有失败的话,会打印一行“未定义”的,为那些老布朗汉克兄弟执行。。。猜测Java让我认为数组需要一个数组标识符,以便知道要保存哪些变量。我的问题就像另一个答案一样,这并没有解决问题,您只是从创建一个用1个元素初始化的数组变为一个空数组。在makeTable()
中添加创建的学生后,它仍然不会导致出现空表,Patrick。根据数据,这