【发布时间】:2021-05-15 15:42:45
【问题描述】:
【问题讨论】:
-
能否请您添加一个代码示例,它可以帮助其他人以有效的方式帮助您
-
@AyushSharma,我没有代码示例,因为我不知道它是否可能。
标签: javascript html css text
【问题讨论】:
标签: javascript html css text
您可以通过将空行替换为空的 HTML 元素来实现:
const text = `this is an example
of multiline text.
spacing between these lines
is consistent, but line below will be different
even multiple empty lines:
will have the same height`;
const div = document.getElementById("main");
div.textContent = text;
div.innerHTML = div.innerHTML.replace(/([\n]{2,})/g, "<p>$1</p>");
console.log("original text length: ", text.length);
console.log("text from html length: ", div.textContent.length);
console.log("are both texts equal? ", div.textContent === text);
div
{
white-space: pre;
line-height: 2em;
}
p
{
height: 2em;
margin: 0;
padding: 0;
border: none;
line-height: 0;
visibility: hidden;
white-space: none;
}
<div id="main"></div>
【讨论】: