简体   繁体   English

检测浏览器/ css强制换行符中的换行符

[英]Detecting line-breaks in browser/css-forced line breaks

<p style="width:60px"> I am some random text. I am Some text.blabla</p>

The rendered HTML result of above might be, 上面呈现的HTML结果可能是,

I am some ra
ndom text. I
am Some text
.blabla

I would like to split the above paragraph into separate paragraphs row by row. 我想将上一段分成一行一行。 So what the expected result is like, 那么预期的结果如何,

<p style="width:60px">I am some ra</p>
<p style="width:60px">ndom text. I</p>
<p style="width:60px">am Some text</p>
<p style="width:60px">.blabla</p>

Is it possible to implement it in Javascript? 是否可以在Javascript中实现它?

PS Why do I have to split it? PS为什么我要分开它? Because I would like to get my own pagination system for a long HTML, where all the paragraphs and splited paragraphs within a height range would be put in to a same <div class="page"></div> . 因为我想为长HTML提供我自己的分页系统,其中高度范围内的所有段落和分段段落将被放入相同的<div class="page"></div> Finally, the whole HTML would be organized as 最后,整个HTML将被组织为

<div id="page1" class="page">
   <p> complete content </p>
   <p> upper part of XXX </p>
</div>

<div id="page2" class="page">
   <p> bottom part of XXX </p>
   <p>...</p><p>...</p>
</div>

Well, assume that your text is in a <p id='wholeText'> : 好吧,假设你的文字在<p id='wholeText'>

var element = document.getElementById("wholeText");

var wholeText = element.innerHTML;

var splitText = wholeText.split("\r\n");

var newHtml = null;

for(var i = 0; i < splitText.length; i++)
{
   newHtml += "<p class='each-line'>"+splitText[i]+'</p>';
}

Then you can use newHtml to write in a DOM element. 然后你可以使用newHtml来写一个DOM元素。 For instance, if you like to add it to a the same <p> which you got text from, you do: 例如,如果您想将其添加到您从中获取文本的相同<p> ,则执行以下操作:

element.innerHTML = newHtml; // remember element refers to the <p> with the ID of wholeText

Also put all of the above codes into a function and call the function after the windows has been fully loaded. 还将上述所有代码放入一个函数中,并在窗口完全加载后调用该函数。

window.addEventListener("load", function(){

// put the code here
}, false);

If your line does not contain break characters , then please refer to this plugin : http://jsfiddle.net/nathan/qkmse/ 如果你的行不包含break characters ,那么请参考这个插件: http//jsfiddle.net/nathan/qkmse/

The above link is suggested in this SO's question: 在此SO的问题中建议了上述链接:

detecting line-breaks with jQuery? 用jQuery检测换行符?

If you really want to format the text as if word-wrap:break-word; word-break:break-all; 如果你真的想格式化文本就像word-wrap:break-word; word-break:break-all; word-wrap:break-word; word-break:break-all; were set on the p element (as you say in a comment) and each of the resulting lines is turned to a paragraph, you need code like the following: Process the p element character by character, putting as many characters as fit on one with the given width. p元素上设置(正如你在评论中所说的那样)并且每个结果行都转换为段落,你需要像下面这样的代码:逐个字符处理p元素,在一个字符上放置尽可能多的字符给定的宽度。 When it is exceeded, construct a new p element and proceed with the rest of the text. 超过时,构造一个新的p元素并继续执行其余的文本。 You can do this by writing the characters to an invisible inline block and checking its rendered width. 您可以通过将字符写入不可见的内联块并检查其呈现宽度来完成此操作。

Example (note that this really works only when the paragraph contains just text, no markup): 示例(请注意,只有当段落只包含文本,没有标记时,这才真正起作用):

<p style="width:60px" id=p> I am some random text. I am Some text.blabla</p>
<script>
var width = 60;
var p = document.getElementById('p');
var parent = p.parentNode;
var line = document.createElement('span');
line.style.display = 'inline-block';
line.style.visibility = 'hidden';
var content = p.innerHTML;
document.body.appendChild(line);
var start = 0;
var i = 1;
while(i < content.length) {
  line.innerHTML = content.substring(start, i);
  console.log(i + ' ' + content.length);
  if(line.clientWidth > width) {
     var newp = document.createElement('p');
     newp.style.width = width + 'px';
     newp.innerHTML = content.substring(start, i - 1);
     parent.insertBefore(newp, p);
     start = i - 1;
     i = start + 1;
   }
   else {
     i++;
   }
}
newp = document.createElement('p');
newp.style.width = width + 'px';
newp.innerHTML = content.substring(start);
parent.insertBefore(newp, p);
parent.removeChild(p);
</script>

I'm pretty sure you should have a completely different approach, but I cannot help with the ultimate problem you are trying to solve, as it was not described. 我很确定你应该有一个完全不同的方法,但是我无法帮助解决你想要解决的最终问题,因为它没有被描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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