(我正在學習JavaScript中的原型..) 我需要在div上動態添加學生的詳細信息,並通過使用javascript原型繼續添加更多按鈕(添加更多)時點擊。使用JavaScript原型,需要動態添加div onclick
到目前爲止,這裏是我的代碼:
的Javascript:
function Student(name, age, department) {
this.name = name;
this.age = age;
this.department = department;
}
Student.prototype.addStudent = function() {
var s1 = new Student();
s1.name = document.getElementById('name').value;
s1.age = document.getElementById('age').value;
s1.department = document.getElementById('department').value;
};
這是我的HTML:
<!DOCTYPE html>
<html>
<head>
<title>Prototype_Student</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="../css/student_style.css"/>
</head>
<body>
<div>Student List</div>
Name:<input class="inputFields" id="name"/><br><br>
Age:<input class="inputFields" id="age"/><br><br>
Department:<input class="inputFields" id="department"/><br><br>
<button id="addMore" onclick="addStudent(name, age, department);">Add Student</button>
<script src="../js/student_main.js"></script>
</body>
</html>
我的小提琴:http://jsfiddle.net/2TMuB/
上午陷入其中,我不不知道如何動態創建新的學生對象並顯示它們。
javascript中的原型編程。 對不起,如果我標記另一個..將刪除它。 –
使用'document.createElement();'。你可以在[MDN](https://developer.mozilla.org/en-US/docs/Web/API/document.createElement)上閱讀更多關於它的信息。 – pstenstrm
因此你捕獲了你的s1 var和現在的信息?您需要將其轉儲到某個地方,例如通過動態構建學生條目列表。 jQuery允許簡單的動態DOM添加。 – mvw