简体   繁体   English

我已经尝试了createTextNode的每个换行符,但是我不能

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

/n doesn't work on my code as it doesn't print out a line break. /n在我的代码上不起作用,因为它不会打印出换行符。 Please help! 请帮忙!

</br> doesn't work </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>

This is what I get: 这是我得到的:

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 110 120 1122 33 44 55 66 77 88 99 110 121 132 1224 36 48 60 72 84 96 108 120 132 144 13 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

The problem is you are making a table with a single table row. 问题是您要制作一个带有单个表行的表。 If you want it to be in multiple rows, you need multiple tr . 如果希望它位于多行中,则需要多个tr

This is what you currently have. 这就是您目前拥有的。

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

This is what you seem to want. 这就是您似乎想要的。

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

You can try the studying the following code as an example 您可以尝试研究以下代码作为示例

This will create a table with the numbers 1 - 50 with 5 rows and 10 columns. 这将创建一个数字表1-50,具有5行和10列。

I do not recommend using \\t for separation or visual type of stuff. 我不建议将\\t用于分隔或视觉化的东西。 You should consider using CSS for styling spacing and whatnot. 您应该考虑使用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.

相关问题 如何在createTextNode中插入换行符 - How to insert a line break in createTextNode 为什么不能以短代码将Web Clip图标添加到我的Web应用程序中。 我已经尝试了一切? - Why can't I add a web clip icon to my webapp in dashcode. I've tried everything? 为什么我的 javascript 不能重定向以下代码,我尝试了多种解决方案,我在 StackOverFlow 中找到了 - Why can't my javascript redirect the following code, I've tried multiple solution that i found in StackOverFlow 无法计算表格行的总和我从互联网上尝试了很多方法但没有 - Can't calculate the sum of table rows I've tried many ways from the internet but nothing 如何使用JavaScript更改CreateTextNode的样式 - How can I change the style of CreateTextNode With JavaScript 我尝试了一些 ajax 请求,但它不起作用 - I've tried to some the ajax request but it won't work 如何在 google docs 按钮中换行? - How can I make a line break in a google docs button? 如何制作新行或在数组中中断? - How can I make new line or break in array? 无法提交表单操作,因为它被覆盖隐藏了 - 我试过 Z-Index 和 Pointer-events:none; CSS/反应 - Can't submit Form action since it is hidden by overlay - I've tried Z-Index and Pointer-events:none; CSS/React 我已经在CSS中隐藏了一些内容,但是当我单击按钮时我现在无法使其可见 - I've made something hidden in css, i can't make it visible now when I click a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM