簡體   English   中英

我已經嘗試了createTextNode的每個換行符,但是我不能

[英]I've tried every line break for createTextNode but I can't make one

/n在我的代碼上不起作用,因為它不會打印出換行符。 請幫忙!

</br>不起作用

<script> 
        var x = document.createElement("TABLE");
        x.setAttribute("id", "myTable");
        document.body.appendChild(x);

        var y = document.createElement("TR");
        y.setAttribute("id", "myTr");
        document.getElementById("myTable").appendChild(y);

        var z = document.createElement("TD");

        var column = 1
        for(row = 1; row < 13; row++){
            var product = column*row;
            if(column != 13){
                var t = document.createTextNode(product + " ")
                if(row == 12){
                    column += 1
                    var t = document.createTextNode(product + " \u000a" + column)
                    row = 1;
                }
            }
            else if(column == 13){
                break
            }
            z.appendChild(t);
            document.getElementById("myTr").appendChild(z);
        }
    </script>

這是我得到的:

1 2 3 4 5 6 7 8 9 10 11 12 24 6 8 10 12 14 16 18 20 22 24 36 9 12 15 18 21 24 27 30 33 36 48 12 16 20 24 28 32 36 40 44 48 510 15 20 25 30 35 40 45 50 55 60 612 18 24 30 36 42 48 54 60 66 72 714 21 28 35 42 49 56 63 70 77 84 816 24 32 40 48 56 64 72 80 88 96 918 27 36 45 54 63 72 81 90 99 108 1020 30 40 50 60 70 80 90 100 110120 1122 33 44 55 66 77 88 99 110121 132 1224 36 48 60 72 84 96 108 120 132 144 13

問題是您要制作一個帶有單個表行的表。 如果希望它位於多行中,則需要多個tr

這就是您目前擁有的。

<table id="myTable">
  <tr id="myTr">1\n 2\n 3\n</tr>
</table>

這就是您似乎想要的。

<table id="myTable">
   <tr id="myTr">1 2 3 4</tr>
   <tr>3 4 5 6</tr>
</table>

您可以嘗試研究以下代碼作為示例

這將創建一個數字表1-50,具有5行和10列。

我不建議將\\t用於分隔或視覺化的東西。 您應該考慮使用CSS設置間距樣式以及諸如此類。

 var table = document.createElement("table"); table.setAttribute("id", "myTable"); const ROWS = 5; const COLUMNS = 10; var product = 1; for (var i = 0; i < ROWS; i++) { // Create the rows element var row = document.createElement("tr"); for (var j = 0; j < COLUMNS; j++) { // Put the "product" in the row. \\t is the tab seperator, for easy view. row.appendChild(document.createTextNode(product+"\\t")); product += 1; } // Append the row with the data in the table. table.appendChild(row); } // Append the table to the body element to view. document.body.appendChild(table); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM