简体   繁体   English

删除div中的空白行

[英]Remove blank lines in div

I have a tag: 我有一个标签:

<pre>
     this is a

    this is b


       this is c
</pre>

After parsed by browser, it outputs: 在浏览器解析之后,它输出:

     this is a

    this is b


       this is c

What I want is: 我想要的是:

this is a
this is b
this is c

Is there any way to only use client methods to do this? 有什么方法只能使用客户端方法来执行此操作吗?

Change <pre> tag to <div> ? <pre>标签更改为<div>吗? But it turns to single line, not I want. 但这变成了单行,不是我想要的。

Add css? 添加CSS? I did not find a style to achieve. 我没有找到实现的风格。

Or use javascript? 还是使用JavaScript?

PS : the content in <pre> tag is generated by liquid template {{ post.content}} , it is immutable . PS<pre>标签中的内容是由液体模板{{ post.content}} ,它是不可变的

Trim and filter out empty lines. 修剪并过滤出空行。

 // get the pre tag var pre = document.querySelector('pre'); pre.innerHTML = pre.innerHTML // split by new line .split('\\n') // iterate and trim out .map(function(v) { return v.trim() // filter out non-empty string }).filter(function(v) { return v != ''; // join using newline char }).join('\\n') 
 <pre> this is a this is b this is c </pre> 

You are using pre tag which mean preformatted text, anything included between this tag whether it's space or line-break , the output will be same as included. 您正在使用pre tag ,即预格式化的文本,此标签之间包含的任何内容(无论是space还是line-break ,输出都将与包含的内容相同。

The HTML element represents preformatted text. HTML元素表示预格式化的文本。 Text within this element is typically displayed in a non-proportional ("monospace") font exactly as it is laid out in the file. 此元素内的文本通常以与文件中所布置的完全非比例(“等宽”)字体显示。 Whitespace inside this element is displayed as typed. 此元素内的空格显示为已键入。

So you could make use of some other tag, still you could remove space between those sentences using JavaScript as added by Pranav C Balan 因此,您可以使用其他一些标记,但仍然可以使用Pranav C Balan添加的JavaScript删除这些句子之间的空间

 <pre> Hi xyz demo text.Hi xyz demo text. Hi xyz demo text. Hi xyz demo text. </pre> 

dont use "pre" tag. 不要使用“ pre”标签。 use simple 使用简单

<div>this is a</div>
<div>this is b</div>
<div>this is c</div>

i hope this helps 我希望这有帮助

<p>
 this is a<br/>
 this is b<br/>
 this is c<br/>
</p>

with HTML, you can do this! 使用HTML,您可以做到这一点! Pre tag in HTML prints as it is without a word-wrap and with whitespace in tact. HTML中的pre标签可以直接打印,没有自动换行,并且带有空格。

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

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