admin 管理员组文章数量: 1086019
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>
? But it turns to single line, not I want.
Add css? I did not find a style to achieve.
Or use javascript?
PS: the content in <pre>
tag is generated by liquid template {{ post.content}}
, it is immutable.
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>
? But it turns to single line, not I want.
Add css? I did not find a style to achieve.
Or use javascript?
PS: the content in <pre>
tag is generated by liquid template {{ post.content}}
, it is immutable.
4 Answers
Reset to default 4Trim 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.
The HTML element represents preformatted text. 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
<pre>
Hi xyz demo text.Hi xyz demo text.
Hi xyz demo text.
Hi xyz demo text.
</pre>
dont use "pre" tag. 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! Pre tag in HTML prints as it is without a word-wrap and with whitespace in tact.
本文标签: javascriptRemove blank lines in divStack Overflow
版权声明:本文标题:javascript - Remove blank lines in div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744095631a2532846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论