2017-01-08 92 views
0

我想不通爲什麼這段代碼沒有工作。我究竟做錯了什麼?我需要使用document.write在Javascript中創建我的表格。任何幫助,將不勝感激!試圖讓HTML表格出現在Javascript中的document.write中

<!DOCTYPE html> 

<html> 

<head> 
<title></title> 
</head> 

<body> 

<script type="text/javascript"> 

var rows; 
var cols; 

function table(rows, cols){ 
document.write('<table border="1">'); 
    for (i=0, i < rows, i++){ 
    document.write('<tr>'); 

    for (j=0, j < cols, j++) { 
    document.write('<td>' + 'cell' + '</td>'); 
    } 
    document.write('</tr>'); 
} 
document.write('</table>'); 
} 

document.write (table(2, 4)); 

</script> 

</body> 

</html> 
+1

不要使用'document.write'!你爲什麼使用逗號?你爲什麼要寫'undefined'? – Li357

+0

首先查看瀏覽器開發工具控制檯中的錯誤。你有邏輯問題,但也有控制檯會指示你的語法問題 – charlietfl

+0

我不推薦使用'document.write();'但是這個不工作的原因是因爲你的for()'循環。用''替換''''''例如'i = 0; I <行; i ++' – NewToJS

回答

2

好了,現在你有一個耳朵充滿document.write,請參閱this這個話題。

一覽你的語法是好的有兩個明顯的例外是,在for循環中,我們使用分號,因爲for循環就好的3條語句的簡寫形式,我們有什麼語句後這樣瀏覽器就知道你是成品陳述一個聲明是;

for(let i=0; i < rows; i++) {...} 

另一個明顯的錯誤是你如何調用末的功能,它應該是:

table(2,4); 

下面的代碼演示創建的一種替代方法表。詳細信息在Snippet中進行了評論。

代碼片段

<!DOCTYPE html> 
 

 
<html> 
 

 
<head> 
 
    <title></title> 
 
</head> 
 

 
<body> 
 

 
    <!--We don't have to add "type="text/javascript"... 
 
<script> tags anymore--> 
 
    <script> 
 
    var rows; 
 
    var cols; 
 

 
    function createTable(rows, cols) { 
 
     /* Using creatElement() method is simple and fast... 
 
     || ...but be mindful that even though you created... 
 
     || ...an element, it's not going to showup until... 
 
     || ...it is added to the DOM. 
 
     */ 
 
     var table = document.createElement('table'); 
 

 
     /* At this point we have a <table> that's... 
 
     || ..."floating around". This is the most... 
 
     || ...advantageous time to add any attributes... 
 
     || ...and other elements. It's costly to add... 
 
     || ...to add to the DOM, so while any element... 
 
     || ...is detached from the DOM (i.e. <table>,)... 
 
     || ...add as much as you are able to before... 
 
     || ...adding it to the DOM. 
 
     */ 
 
     table.setAttribute('border', '1'); 
 

 
     /* for loop = 3 statements + 2 semicolons */ 
 
     // let ~i~ equal 0; 
 
     // while ~i~ is less than ~rows~ continue looping; 
 
     // at the end of a loop increment ~i~ by 1; 
 
     for (let i = 0; i < rows; i++) { 
 

 
      // Now we have a detached <tr>... 
 
      var tRow = document.createElement('tr'); 
 

 
      // ...which moves on to an inner loop 
 
      for (let j = 0; j < cols; j++) { 
 

 
      // A detached <td> 
 
      var tData = document.createElement('td'); 
 

 
      // While <td> is detached, let's add text 
 
      tData.textContent = 'Cell'; 
 

 
      /* appendChild() method will add the <td>... 
 
      || ...to the <tr>. This inner for loop will... 
 
      || ...continue to add <td> to this <tr>... 
 
      || ...as many times the number of ~cols~... 
 
      || ...That was set by this statement: 
 
      || j < cols; and it's perpetuated by this: 
 
      || j++ 
 
      */ 
 
      tRow.appendChild(tData); 
 
      } 
 
      /* Once the <tr> has completed all of the... 
 
      || inner loops, <tr> is now added to <table>... 
 
      || ...That is one complete loop of the outer 
 
      || ...loop. That's one <tr> filled with <td>... 
 
      || ...This whole cycle will go on until... 
 
      || ...whatever number ~rows~ is. 
 
      */ 
 
      table.appendChild(tRow); 
 
     } 
 
     /* After the last outer loop, the <table> is... 
 
     || ...completed and added to the <body> which... 
 
     || ...already is attached to the DOM, thus... 
 
     || ...<table> is in the DOM as well. 
 
     */ 
 
     document.body.appendChild(table); 
 
     } 
 
     // Call the function and pass 2 and 4. 
 
    createTable(2, 4); 
 
    </script> 
 

 
</body> 
 

 
</html>

相關問題