需要阅读的内容很多,但如果您全部阅读,它应该是有意义的。
我正在开发一款要求用户在 iOS 上突出显示文本的应用。文本分为段落,有换行符和段落符,我决定只使用 <br />
换行符的标签,并开始一个新的 p
每个段落的元素。 Mobile Safari 的问题在于,当您选择的元素不是内联时,您无法单独选择字母。相反,尝试突出显示文本会突出显示段落的整个 block ,因为它是显示 block ..
为了解决这个问题,我决定用像这样的内联元素替换所有换行符 ( \n
):
.space{
height:0px;
display:inline-block;
width:100%;
}
所以 HTML 看起来像:
Line one
<span class="space"></span>
Line two
并且会输出为:
Line One
Line Two
我想,“干得好,你想通了!”
跳到今天,我发现这不是我想要的行为。由于用户突出显示的文本来自处理为纯文本的 PDF 文件,因此会出现如下情况:
Hello there, this is a paragraph and\n
it is continuing onto the next line and this line will continue and be wrapped by the set width of the parent
我将处理为
Hello there, this is a paragraph and
<span class="space"></span>
it is continuing onto the next line and this line will continue and be wrapped by the set width of the parent
输出为
Hello there, this is a paragraph and
it is continuing onto the next line and this line will continue and be wrapped by the set width of the parent
那显然不好!现在有一个全新的“段落分隔符”,应该有一个“换行符”。所以,我想我会创建两个不同的内联“中断”类,一个用于单个空格,一个用于双空格。
双空格元素的作用与上面的一样,而单空格元素只是将内容移到下一行。所以,这让我想到了我的实际问题:
如何使用行内定位元素将文本移动到下一行?
此外,我无法将文本包裹在另一个元素中,例如 span
,所以我只能用CSS做一个内联换行元素和段落元素。
我尝试了一些事情来让它发挥作用。而不是设置 width
单个空间元素到 100%
就像我对双倍空间元素所做的那样,我可以改为计算容器的宽度和文本占用的宽度,然后将两者相减,得到剩余空间的宽度。这将允许我将内容推送到新行。
你可以在这里测试这个方法:http://jsfiddle.net/charlescarver/ksvkk/12/
此方法的问题在于,虽然我可以确定宽度,但无法确定多行文本节点的宽度。此外,如果容器大小发生变化,这也不灵活。
一个可能的想法是让单个空格元素具有 100% width
,但我无法开始工作。 ,并让文本推送它,以便将换行符向下推送到下一行。就像我说的,我无法让它工作。
最佳答案
这似乎为内联中断提供了解决方案:http://codepen.io/pageaffairs/pen/oliyz
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style media="all">
[data]:after {
content: attr(data);
white-space: pre;
height: 0;
}
.doublespace{
height:0;
display:inline-block;
width:100%;
}
</style>
</head>
<body>
<p>Hello there, this is a paragraph and <b data='
'></b> it is continuing onto the next line and this line will continue and be wrapped by the set width of the parent</p>
<p>Hello there, this is a paragraph and
<span class="doublespace"></span>
it is continuing onto the next line and this line will continue and be wrapped by the set width of the parent</p>
</body>
</html>
在这个例子中,只有文本被选中,而不是整个 block 。我没有在 iPhone 上测试过,但提供它以防万一,因为这是一个有趣的问题。
关于javascript - 内联换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16754375/