我目前在预代码块中有以下 html:
<pre class="prettyprint"><code>
<html>
<body>
<form name="input" action="html_form_action.asp" method="get">
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female<br>
<input type="submit" value="Submit">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a page called "html_form_action.asp".</p>
</body>
</html>
</code></pre>
它在 html 源代码中缩进,以便在文档中获得更好的结构。如何删除前导空格?通过使用javascript还是有更简单的方法。
最佳答案
问题询问是否有 JavaScript 解决方案或更简单的方法来删除前导空格。有一个更简单的方法:
CSS
pre, code {
white-space: pre-line;
}
The
white-space
property is used to describe how whitespace inside the element is handled.pre-line
Sequences of whitespace are collapsed.
为了保留现有的空白或缩进,每一行都可以包含在子标记中,并在行级别将 white-space
重置为 pre
:
HTML
<pre>
<code>
<i>fn main() {</i>
<i> println!("hello world!");</i>
<i>}</i>
</code>
</pre>
CSS
pre, code {
white-space: pre-line;
}
pre > code > i {
white-space: pre;
}
关于javascript - 从 pre/code 标签中缩进的 HTML 源代码中删除前导空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17257200/