简体   繁体   English

如何从 JavaScript 开始一个新行?

[英]How to start a new line from JavaScript?

I have created this "shell" in jQuery:我在 jQuery 中创建了这个“shell”:

Code: link text代码:链接文本

Demo: link text演示:链接文本

I need that every 100 chars, the prompt go in a new line below.我需要每 100 个字符,提示在下面换一个新行。 How can I do this ?我怎样才能做到这一点 ?

At a high level, you're generating html from JavaScript, so any html which would work standalone will work when generated from JavaScript too.在高层次上,您是从 JavaScript 生成 html,因此任何可以独立工作的 html 在从 JavaScript 生成时也可以工作。 Some possibilities, with varying flexibility:一些可能性,具有不同的灵活性:

Pre-formatted text, insert newline "\n" characters:预格式化文本,插入换行符"\n"字符:

<pre>
line one
line two
</pre>

Inserting break tags:插入中断标签:

line one<br />
line two<br />

Or my favourite, use paragraph tags:或者我最喜欢的,使用段落标签:

<p>line one</p>
<p>line two</p>

I like this because you can refer to each line in code as a single DOM element.我喜欢这样,因为您可以将代码中的每一行称为单个 DOM 元素。 Set this css to keep the lines side-by-side:设置此 css 以保持行并排:

div.code-listing p { margin: 0; padding: 0; }

Programaticaly, by concatenating <br /> is the only way I can think of.编程上,通过连接<br />是我能想到的唯一方法。

word-wrap: break-word;

Combined with a width should give you an approximation, but it won't be perfect.结合宽度应该会给你一个近似值,但它不会是完美的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM