简体   繁体   English

表示空白区域中的自动换行:预换元素

[英]indicate automatic line break in white-space: pre-wrap element

I'm using white-space: pre-wrap style on a HTML <pre> element to allow lines to break when they are longer than the browser window is wide. 我在HTML <pre>元素上使用white-space: pre-wrap样式,以允许行在比浏览器窗口宽时更长时断开。

Unfortunately those broken lines also look as if they have a line break at the end; 不幸的是,那些断线也看起来好像在最后有一个换行符; the user cannot see if it was an automatic line break. 用户无法看到它是否是自动换行符。

Is there a way to show either at the end of the line that wrapping is going on (as emacs does with a \\ character), or at the beginning of wrapped lines that they are a continuation of a previous line (eg with )? 有没有办法在行的末尾显示正在进行包装(如emacs使用\\字符),或者在包装行的开头显示它们是前一行的延续(例如,使用 )?

Copying & pasting should not copy the continuation characters. 复制和粘贴不应复制延续字符。


Example code : 示例代码

<pre style="white-space: pre-wrap">for i in range(19): selwidth=5; selheight=1000; image = gimp.image_list()[0];posx=initx+i*90; pdb.gimp_image_select_polygon(image, 2, 8, [posx, 0, posx+selwidth, 0, posx+selwidth-selheight, selheight, posx-selheight, selheight]);</pre >

Preferred rendering, with a at the beginning of continuation lines: 首选渲染,在延续线的开头有

for i in range(19): selwidth=5; selheight=1000; image = gimp.image_list()[0];posx=
→initx+i*90; pdb.gimp_image_select_polygon(image, 2, 8, [posx, 0, posx+selwidth, 0, 
→posx+selwidth-selheight, selheight, posx-selheight, selheight]);

Pre is intended to keep text as it was typed. Pre用于保存键入的文本。 It helped keep poems and speciality text as they were intended to be seen and not formatted by the browser. 它有助于保留诗歌和专业文本,因为它们有意被浏览,而不是由浏览器格式化。 I would think most people will be able to tell a line of text is being wrapped in Pre with whitespace: pre-wrap because it would look something like this: Five little monkeys jumping on the bed, {{line break}} One fell off and bumped his head. 我想大多数人都能说出一行文字用Pre包裹在Pre中:预包装,因为它看起来像这样:五只小猴子跳到床上,{{line break}}一个掉下来并撞了他的头。 {{line break}} Mama called the Doctor and the Doctor said,{{line break}} "No more monkeys jumping on the bed!'.{{line break}} {{line break}} Mama打电话给医生,医生说,{{line break}}“没有更多的猴子在床上跳!”{{line break}}

If you went with straight HTML <p> it would look as you had typed it in your example and <pre> with whitespace: pre-wrap would look the space as you have it typed. 如果您使用直接HTML <p>它看起来就像您在示例中键入它一样,而<pre>使用空格:预换行将在您输入时查看空格。

To color the ends of each line you might try putting a <span> and give it CSS to color and size or do a <span> on the whole sentence giving it a CSS background color. 要为每条线的末端着色,您可以尝试使用<span>并将CSS赋予颜色和大小,或者在整个句子上执行<span> ,为其提供CSS背景颜色。

AFAIK not with CSS, instead you can replace every newline with 2 newlines so newlines will be distinguished when text wraps, to do this either manually enter two - or more - line-breaks <br> s for each new line, or if you can use javascript then you can replace each semi-colon ; AFAIK不使用CSS,而是可以用2个换行替换每个换行符,以便在文本换行时区分换行符<br>为每个新行手动输入两行或更多行换行符,或者如果可以使用javascript然后你可以替换每个分号; - because the provided example in the question is code where each line ends with ; - 因为问题中提供的示例是每行结尾的代码 ; - replace it with ;\\n\\n - or with ;<br><br> instead - thus it will recognized. - 替换为;\\n\\n - 或者用;<br><br>替换它 - 因此它将被识别。

JS Fiddle JS小提琴

 var pre = document.getElementsByTagName('pre')[0], preHTML = pre.innerHTML; pre.innerHTML = preHTML.replace(/;\\s*/g, ";\\n\\n"); 
 <pre style="white-space: pre-wrap">for i in range(19): selwidth=5; selheight=1000; image = gimp.image_list()[0];posx=initx+i*90; pdb.gimp_image_select_polygon(image, 2, 8, [posx, 0, posx+selwidth, 0, posx+selwidth-selheight, selheight, posx-selheight, selheight]);</pre > 

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

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