简体   繁体   中英

how to add a line break in javascript that works with paragraph

I wrote this function to format a certain string :

var desc = document.getElementById('desc');
            var parContent = desc.textContent.trim();
            var tempPar = "";
            var j = 0;
            var k = 0;
            var built_val = "";

            for (var i = 0; i < parContent.length ; i++)
            {
                if (j == 19 || i == parContent.length-1)
                {
                    tempPar = parContent.substring(k, i);
                    tempPar = tempPar.concat("- \n");
                    built_val = built_val.concat(tempPar);

                    tempPar = "";
                    //Restart j
                    j = 0;
                    //update k
                    k = i;
                    continue;
                }
                j++;
            }
            desc.textContent = built_val;

Desc is a dynamic paragraph that is usually empty at first then filled (its data are composed after the page loads), j is the number of characters desired in one line.

Though now I have another problem and that is \\n doesn't work ; I also tried br
. How can I insert a new linebreak within a javascript string such as "built_val" ? please note how it's assigned to an Html

after everything.

The textContent property sets the literal text of the element (by adding a text node), and will not parse your tags as html. Instead, you should do this:

desc.innerHTML = built_val;

Yes, you're using .textContent which is why <br> s won't be parsed (which is a good thing!)

You want to use document.createTextNode() and document.createElement('br') .

var desc = document.getElementById('desc');
var parContent = desc.textContent.trim();
var tempPar = "";
var j = 0;
var k = 0;
var built_val = "";

for (var i = 0; i < parContent.length; i++) {
    if (j == 19) {
        tempPar = parContent.substring(k, i);
        tempPar = tempPar.concat("- \n");
        desc.appendChild(document.createTextNode(tempPar)); desc.appendChild(document.createElement('br'));

        tempPar = "";
        //Restart j
        j = 0;
        //update k
        k = i;
        continue;
    }
    j++;
}
// No need for textContent anymore. Appending nodes did the work for us!

Just for fun: an Array.forEach and String.slice method:

 var desc = document.querySelector('#desc'); var parContent = desc.textContent.trim(); var block = 0; parContent.split('').forEach( function(v, i) { if (i % 19 == 0) { desc.appendChild(document.createTextNode(parContent.slice(block, i))); desc.appendChild(document.createElement('br')); block = i; } } ); // last part desc.appendChild(document.createTextNode(parContent.slice(block))); 
 <p id="desc"> This string should be truncated every 19th position right? Ok, let's give it a try using [Array.forEach]<br> </p> 

There's an easy regex version to wrap text as well:

function Wrap(src, maxLineLength) {
    return src.replace(new RegExp("(.{1,"+maxLineLength+"})", "g"), "$1<br/>\r\n");
}

Though this wraps on characters, not words.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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