我试图在html+css (没有javascript)中呈现源代码,以便:
我找到的解决方案使用CSS计数器和一个表,其中最左边的列是一个数据-psuedo-内容(也被标记为不可选)。它在Firefox、Safari和Chrome中正确呈现,但是将文本复制到剪贴板上存在问题。
有一个演示弹琴显示了这个问题。试图将文本复制到Firefox中的剪贴板,在每一行(即额外的换行符)之间放置一个空行。
我如何解决这个问题(只使用html+css)?
.code {
background-color: none;
border: none;
padding: 0;
}
pre.code {
line-height: 1.6;
white-space: pre-wrap;
width: 100%;
margin: 0 auto;
font-size: 14px;
}
;
pre.code table {
counter-reset: linenum;
}
pre.code td.lnum:before {
content: attr(data-psuedo-content) counter(linenum);
}
pre.code td.content {
font-size: 14px;
background: #333740;
color: #ffffff;
white-space: pre-wrap;
padding: 3px;
border-right: solid 2px black;
}
td.lnum {
background-color: #a7a8aa;
color: #000000;
border-right: 2px solid black;
border-top: 1px solid black;
border-bottom: 1px solid black;
border-left: none;
font-size: 12px;
}
pre.code tr {
counter-increment: linenum;
}
.lnum {
-moz-user-select: none;
webkit-user-select: none;
ms-user-select: none;
}<pre class="code">
<table style="width: 100%;border-collapse: collapse">
<tr><td class="lnum"></td><td class="content">#include <stdint.h></td></tr>
<tr><td class="lnum"></td><td class="content">#include <stdbool.h></td></tr>
<tr><td class="lnum"></td><td class="content"></td></tr>
<tr><td class="lnum"></td><td class="content">/*-</td></tr>
<tr><td class="lnum"></td><td class="content"> | Support for x86 operations that are not exposed natively in C. Each of these</td></tr>
<tr><td class="lnum"></td><td class="content"> | is a fragment of inline-assembly (a way of injecting assembly code into the</td></tr>
<tr><td class="lnum"></td><td class="content"> | compiled program). Each one is wrapped in an inline procedure so that the </td></tr>
</table>
</pre>
发布于 2018-07-04 11:47:31
不要添加这段代码
.lnum {
-moz-user-select: none;
webkit-user-select: none;
ms-user-select: none;
}发布于 2021-06-01 00:41:36
我使用JavaScript来挂起复制事件:
const source = document.querySelector('pre.code');
source.addEventListener('copy', (event) => {
const selection = document.getSelection();
event.clipboardData.setData('text/plain', selection.toString());
event.preventDefault();
});https://stackoverflow.com/questions/51170894
复制相似问题