วิธีแรก:
การคัดลอกรหัสมีดังนี้:
<html>
<head>
<script>
ฟังก์ชั่น createTable (แถวบรรทัด) {
this.rows = แถว;
this.lines = lines;
var body = document.getElementById ('body');
var table = document.createElement ('ตาราง'); // สร้างองค์ประกอบแท็กตาราง
table.setAttribute ('ชายแดน', '1');
// เพิ่มแอตทริบิวต์อื่น ๆ ลงในแท็กตาราง
สำหรับ (var i = 0; i <this.rows; i ++) {
var lrow = document.createElement ('tr');
สำหรับ (var j = 0; j <this.lines; j ++) {
var textNode = document.createtextNode (i+','+j);
var lline = document.createElement ('td');
lline.AppendChild (textNode);
lrow.AppendChild (LLINE);
-
Table.AppendChild (Lrow);
-
Body.appendchild (ตาราง);
-
</script>
</head>
<body>
<div id = "body"> </div>
</body>
<script type = "text/javascript">
CreateTable (10,10);
</script>
</html>
วิธีที่สอง:
การคัดลอกรหัสมีดังนี้:
<script>
ฟังก์ชั่น createTable (แถวบรรทัด) {
this.rows = แถว;
this.lines = lines;
var body = document.getElementById ('body');
var table = document.createElement ('ตาราง');
table.setAttribute ('ชายแดน', 1);
สำหรับ (var i = 0; i <this.rows; i ++) {
var row = table.insertrow (i);
สำหรับ (var j = 0; j <this.lines; j ++) {
เซลล์ var = row.insertcell (j);
cells.innerhtml = i+','+j
-
-
Body.appendchild (ตาราง);
-
</script>
คุณทุกคนเข้าใจสองวิธีข้างต้นหรือไม่? หากมีวิธีที่ดีกว่าโปรดฝากข้อความไว้เพื่ออธิบายและดำเนินการด้วยกัน