[英]Javascript: Add Break after each 100 characters
If I write to long text for example this: 例如,如果我写长文本:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
The text is going out of the page, I have an idea to fix it, after every 100 characters I could make a <br />
tag. 文本不在页面中,我有一个解决的办法,每100个字符后我就可以做一个
<br />
标签。 But I don't know how to do it. 但是我不知道该怎么做。
Thanks for any help! 谢谢你的帮助!
Just use the following CSS property on the element that you wish to force wrapping in: 只需在您要强制换行的元素上使用以下CSS属性:
word-wrap: break-word;
No need for any JavaScript! 无需任何JavaScript!
If you really want to use JavaScript (for concept; CSS is better for accessibility and ease) then go ahead. 如果您真的想使用JavaScript(就概念而言; CSS对于可访问性和易用性而言更好),那么请继续。
Oh, and another thing, if you're using a font that isn't monospace (but rather, proportional), cutting off at 100 characters could be ineffective. 哦,另一件事,如果您使用的字体不是等宽字体(而是成比例的),则截断100个字符可能无效。 One line could have 100 'i' characters, with the next having another hundred 'm' chars, which are very different in size.
一行可能包含100个“ i”字符,下一行可能包含另外100个“ m”字符,其大小差异很大。
Oh, and another thing, you can't just apply a regex replace on the innerHTML
unless it's all text. 哦,还有一件事,您不能仅在
innerHTML
上应用正则表达式替换,除非它全部是文本。 If there could possibly be other elements there, you must actually loop through the nodes, applying the technique to only text nodes. 如果那里可能还有其他元素,则必须实际上遍历节点,仅将技术应用于文本节点。
Oh, and another thing, don't bother. 哦,还有一件事,不要打扰。 Too many problems doing it with javascript.
使用javascript进行处理时出现太多问题。
You can try the following code 您可以尝试以下代码
str = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
len = str.length;
loop = len / 100;
document.write(loop);
document.write('<br>');
for(i=0; i<=loop; i++){
document.write( str.slice( i*100, (i*100)+100) );
document.write('<br>');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.