Javascript 如何用'-';?

Javascript 如何用'-';?,javascript,for-loop,text,replace,split,Javascript,For Loop,Text,Replace,Split,我有这样的字符串: var content=“Lorem ipsum door sit amet,concertetur adipising Elite,在劳工和大型企业中暂时使用eiusmod 如果你是一个小人物,那你就去实习吧 实验室是从一个普通消费品中提取的,是从一个或多个地方提取的 这是一个充满活力的故事,它是一个充满活力的故事 不可忽视的例外情况,必须在 这是我的错,因为我的动物是我的劳动。” 句子将以左平面html格式显示,如下所示: Lorem ipsum dolor sit am

我有这样的字符串:

var content=“Lorem ipsum door sit amet,concertetur adipising Elite,在劳工和大型企业中暂时使用eiusmod 如果你是一个小人物,那你就去实习吧 实验室是从一个普通消费品中提取的,是从一个或多个地方提取的 这是一个充满活力的故事,它是一个充满活力的故事 不可忽视的例外情况,必须在 这是我的错,因为我的动物是我的劳动。”

句子将以左平面html格式显示,如下所示:

Lorem ipsum dolor sit amet,Concertetur Adipising Elite,在劳工和大型企业中暂时使用eiusmod 阿利夸。我们的工作是最低限度的,我们的工作是实践性的 实验室是一个普通的实验室。二人世界 这是一个充满活力的故事,它是一个充满活力的故事 帕里图尔。除非圣奥卡塔特无过失,必须 这是我的错

如何使用JavaScript将行尾剩余的多余空格替换为负数(-)字符,从而使句子对齐对齐对齐,从而得到这样的结果

Lorem ipsum dolor sit amet,Concertetur Adipising Elite,在劳工和大型企业中暂时使用eiusmod 阿利夸。我们的工作是最低限度的,我们的工作是实践性的 实验室是一个普通的实验室。二人世界 在这本书中,我看到的是一个充满活力的故事 帕里图尔。除非圣奥卡塔特无过失,必须 罪过当然是罪过------这是我的劳动-----------------------------------------------------------------------------------

输出应该是这样的

必须保留报告的原始格式,因此不允许对其进行更改

我试图用这段代码来实现这一点

var html=content.split(“”);
var htmlTmp=“”;
var结果=”;
var limitlength=40;
var linelength=50;
对于(var i=0;i限制长度){
结果+=html[i]+“”;
如果(htmlTmp_长度<线宽){
对于(j=0;j这就是你需要的吗

var LIMIT = 1<<16;
function twTexts (target, content, delim, cw, noOverflow) {
    // Split text into delim
    var array = content.split(delim);
    var span = $("<span></span>");
    target.append(span);
    var loopCount = 0;
    for (var i = 0; loopCount++ < LIMIT && i < array.length; i++) {
        if (i > 0) span.append(delim);
        span.append(array[i]);
        if ((noOverflow && span.width() > target.width()) || span.height() > cw) {
            // Line break caused by this word.
            var text = array.slice(0, i).join(delim);
            span.text(text);
            if (i != 0) {
                // Add '-' until the line breaks
                for (;span.height() <= cw && span.width() <= target.width(); text += '-')
                    span.append('-');
                span.text(text.substring(0, text.length - 1));
                span.append("<br>");
                array = array.slice(i);
            } else {
                // Oops. The line break has happened because of the first word!
                if (array[0].length > 1) {
                    // Let's just break this into characters
                    console.log("Called twTexts");
                    twTexts (target, array[0], '', cw, noOverflow);
                } else {
                    // Ooops. the container's width is narrow than characters. just append this one.
                    span.text (array[0]);
                }
                array = array.slice(i + 1);
            }
            // Start new line
            span = $("<span></span>");
            target.append(span);
            i = -1;
        } else if (i + 1 >= array.length) {
            // No line break occured and this is the last word.
            text = array.join(delim);
            for (;span.height() <= cw && span.width() <= target.width(); text += '-')
                span.append('-');
            span.text(text.substring(0, text.length - 1));
            span.append("<br>");
            break;
        }
    }
}

var content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.fsdfsfdfsfsffaddfadf";

// The target
$(".target").each(function (i, e) {
    var target = $(e).empty();

    // Calculate the height of a character
    var span = $("<span>A</span>");
    target.append(span);
    var cw = span.height();
    span.remove();

    // Print text
    twTexts (target, content, ' ', cw, true);
});
var LIMIT=10)span.append(delim);
追加(数组[i]);
如果((无溢流和&span.width()>target.width())| | span.height()>cw){
//由这个词引起的换行。
var text=array.slice(0,i).join(delim);
span.text(text);
如果(i!=0){
//添加“-”,直到换行
对于(;跨度高度()1){
//让我们把它分成几个角色
console.log(“称为twtext”);
twtext(目标,数组[0],'',连续波,无过流);
}否则{
//哎呀。容器的宽度小于个字符。只需附加这个。
span.text(数组[0]);
}
array=array.slice(i+1);
}
//开行
span=$(“”);
target.append(span);
i=-1;
}else如果(i+1>=array.length){
//没有发生换行,这是最后一句话。
text=array.join(delim);

对于(;跨度高度()如果您只想在结束行强制对齐,可以使用CSS
text align:justify;text align last:justify;white space:pre
,您可能需要使用正则表达式来选择和替换多个空格字符。请尝试并返回更具体的问题。OP正在尝试什么这么做对我来说没有意义。jQuery对你合适吗?@all,我已经更新了question@mnmaxavg太棒了,非常感谢你,我得到了很大的帮助,你真是太不可思议了:Dhello@minmaxavg..我试图在最后一句话修复你的错误解决方案,但我不知道。你能再帮我一次吗..谢谢..)@a、 fauzi已解决。这一次,它还允许您指定是否希望将单词拆分成碎片,而不是让文本溢出容器(目标)的宽度(
noOverflow
参数,布尔值)例如,它看起来像…Lorem ipsum do lor sit amet------太好了…:D因您的解决方案而闻名。非常感谢minmax,您真的帮助了我。致以最良好的问候。。
var LIMIT = 1<<16;
function twTexts (target, content, delim, cw, noOverflow) {
    // Split text into delim
    var array = content.split(delim);
    var span = $("<span></span>");
    target.append(span);
    var loopCount = 0;
    for (var i = 0; loopCount++ < LIMIT && i < array.length; i++) {
        if (i > 0) span.append(delim);
        span.append(array[i]);
        if ((noOverflow && span.width() > target.width()) || span.height() > cw) {
            // Line break caused by this word.
            var text = array.slice(0, i).join(delim);
            span.text(text);
            if (i != 0) {
                // Add '-' until the line breaks
                for (;span.height() <= cw && span.width() <= target.width(); text += '-')
                    span.append('-');
                span.text(text.substring(0, text.length - 1));
                span.append("<br>");
                array = array.slice(i);
            } else {
                // Oops. The line break has happened because of the first word!
                if (array[0].length > 1) {
                    // Let's just break this into characters
                    console.log("Called twTexts");
                    twTexts (target, array[0], '', cw, noOverflow);
                } else {
                    // Ooops. the container's width is narrow than characters. just append this one.
                    span.text (array[0]);
                }
                array = array.slice(i + 1);
            }
            // Start new line
            span = $("<span></span>");
            target.append(span);
            i = -1;
        } else if (i + 1 >= array.length) {
            // No line break occured and this is the last word.
            text = array.join(delim);
            for (;span.height() <= cw && span.width() <= target.width(); text += '-')
                span.append('-');
            span.text(text.substring(0, text.length - 1));
            span.append("<br>");
            break;
        }
    }
}

var content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.fsdfsfdfsfsffaddfadf";

// The target
$(".target").each(function (i, e) {
    var target = $(e).empty();

    // Calculate the height of a character
    var span = $("<span>A</span>");
    target.append(span);
    var cw = span.height();
    span.remove();

    // Print text
    twTexts (target, content, ' ', cw, true);
});