如何忽略 html“代码"中的前导和尾随换行符?阻止使用 css、javascript 或 jquery? [英] How can I ignore leading and trailing linebreaks within an html "code" block using css, javascript or jquery?

查看:27
本文介绍了如何忽略 html“代码"中的前导和尾随换行符?阻止使用 css、javascript 或 jquery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 HTML 源代码中,我有一个如下所示的代码块(我在这个页面上使用了 showdown 和 highlight.js):

double myNumber = (double)4;</code></pre>

我的问题是第一个换行符仍然是代码"块的一部分.这可能是因为封闭的pre"块,但我需要它,因为 highlight.js 期望它(显然也是 HTML5 标准 推荐).代码呈现如下(注意前导换行符):

所以我的问题是,使用 css、javascript 或 jquery,如何从这样的代码"块中删除前导或尾随换行符?

解决方案

你可以使用这个技巧:

pre:first-line {行高:0;}

In my HTML source code, I have a code block like the following (I use showdown and highlight.js on this page):

<pre><code class="cpp">
double myNumber = (double)4;
</code></pre>

My problem is that the first linebreak remains part of the "code" block. It's probably because of the enclosing "pre" block, but I need that there because highlight.js expects it (also apparently the HTML5 standard recommends it). The code is rendered like this (note the leading line break):

So my question is, using css, javascript or jquery, how can I remove leading or trailing linebreaks from "code" blocks like this one?

解决方案

You could use this hack:

pre:first-line {
    line-height: 0;
}

这篇关于如何忽略 html“代码"中的前导和尾随换行符?阻止使用 css、javascript 或 jquery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆