首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NewLine JavaScript代码

NewLine JavaScript代码
EN

Stack Overflow用户
提问于 2014-01-16 18:03:38
回答 1查看 88关注 0票数 1

这里有一个关于数组的工作代码,但是我希望我的输入在新的行中显示,而不是一行。我试过\n但没有运气。

这是我的代码:

代码语言:javascript
复制
<html>
    <meta charset="UTF-8">
    <head>
        <title>Sample Array</title>
        <script>var index = 0;</script>
    </head> 

    <body>


        Input:
        <input type="text" id="input">
        <input type="button" value="GO" onClick="press(input.value)">

        <p id = "display">

        <script>

            var sample = new Array();

            function press(Number)
            {

                if ( Number != '' )
                {
                    sample[index] = Number;
                    document.getElementById("display").innerHTML += sample[index] + "\n";
                    index++;

                }   

                else
                    alert("empty");
            }       

        </script>

    </body>
</html> 

我想在document.getElementById中编码换行符。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-16 18:04:21

您正在处理HTML,它将大多数空白字符视为单个空格。只需连接一个<br>

代码语言:javascript
复制
document.getElementById("display").innerHTML += sample[index] + "<br>";
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21169449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档