在新的一行包装文字 [英] wrapping text words in new line
问题描述
我正在使用以下代码包装长文本,由用户在文本区域中输入以进行评论:
I'm using the below code for wrapping long text, entered by users in a text area for commenting:
function addNewlines(comments) {
var result = '';
while ($.trim(comments).length > 0) {
result += comments.substring(0,70) + '\n';
comments = comments.substring(70);
}
return result;
}
问题显示在下面的屏幕截图中。关于如何解决它的任何想法?我们可以使用 lastindexof()
方法来获取子字符串中的最后一个空格来逻辑地解决这个问题吗?任何人都可以调整这个小代码来使它正确吗?
The problem is shown in the below screen shot. Any ideas on how to solve it? Can we use lastindexof(" ")
method to get the last space in a substring to solve this issue logically? Can anyone tweak this little code to make it right?
推荐答案
我认为通过CSS包装文本是一个更好的解决方案,但是这里有一个链接可能有用的在javascript中包装文字
I believe wrapping a text by CSS is a better solution however there is a link here which may be helpful wrap-text-in-javascript
顺便说一句,我记得有一个JQuery插件用于包装文本google它。
by the way i remember there is a JQuery plugin for wrapping text google it too.
这篇关于在新的一行包装文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!