Search code examples
htmlline-breakshtml-entities

Any HTML entity would remove space?


Due the change line would lead space between characters. I'm using wbr for stop creating space between charaters, like this:

<p><wbr
></wbr>这里有一段很长,<wbr
></wbr>很长的文字;这里<wbr
></wbr>有一段很长,很长<wbr
></wbr>的文字;这里有一<wbr
></wbr>段很长,很长的文<wbr
></wbr>字;</p>

I just wonder if there is a better solution? Maybe... is there any HTML entity could remove all front space?


Solution

  • HTML entities are used to represent reserved characters like angle brackets.

    One objective of web development is to separate style from structure from presentation. That's why this problem must be solved with JavaScript, not HTML.

    To do it, you would simply select the element from the DOM and use the replace method. To replace all instances of whitespace do the following

    var wbr = document.getElementsByTagName("wbr");
    wbr.innerHTML = wbr.innerHTML.replace(/\s/g, '');
    

    To replace only leading and trailing whitespace do this

    wbr.innerHTML = wbr.innerHTML.trim();