admin管理员组文章数量:1392002
I have a script that takes an input string from a user with variable length, typically ~ 400-3000 letters. My script does some modification to the string, and then should print it to the screen. Using document write, everything appears on one line. However, I want to have ~ 100 letters for each output line, so the user won't have to scroll ~ 3000 letters to the right to see the whole text.
The only way I could imagine to do this would be a for loop, but maybe someone know a better solution?
Edit:
This is the for loop I am currently using to achieve this
document.write("<p>",">Position: ", (lower+1) + "-" + (lower +print_screen.length), "<br/>");
// print only 70 chars per line
for(i = 0; i < print_screen.length; i += 69){
document.write(print_screen.substring(i, i+69), "<br/>");
};
document.write("</p>");
I have a script that takes an input string from a user with variable length, typically ~ 400-3000 letters. My script does some modification to the string, and then should print it to the screen. Using document write, everything appears on one line. However, I want to have ~ 100 letters for each output line, so the user won't have to scroll ~ 3000 letters to the right to see the whole text.
The only way I could imagine to do this would be a for loop, but maybe someone know a better solution?
Edit:
This is the for loop I am currently using to achieve this
document.write("<p>",">Position: ", (lower+1) + "-" + (lower +print_screen.length), "<br/>");
// print only 70 chars per line
for(i = 0; i < print_screen.length; i += 69){
document.write(print_screen.substring(i, i+69), "<br/>");
};
document.write("</p>");
Share
Improve this question
edited Jan 27, 2013 at 14:52
ATOzTOA
36k23 gold badges99 silver badges119 bronze badges
asked Jan 27, 2013 at 14:14
user2015601user2015601
2
-
try using css
white-space: pre-wrap
– Salman Commented Jan 27, 2013 at 14:16 - this thread may help stackoverflow./questions/1155678/… – Deepak Kamat Commented Jan 27, 2013 at 14:22
2 Answers
Reset to default 3You can add breaks in the string using replace
and a regular expression. This adds a break at every 100th character:
document.write(theText.replace(/(.{100})/g, '$1<br/>'));
Use this CSS for your container:
.longtext {
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
white-space: pre-wrap; /* css-3 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
word-break: break-all;
white-space: normal;
}
本文标签: javascriptdocumentwrite text over multiple linesStack Overflow
版权声明:本文标题:javascript - document.write text over multiple lines - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744760484a2623726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论