我正在尝试构建一个文本编辑器,在每一行之前显示一个行号。用户可以编辑内容,这要归功于“contenteditable”HTML属性。但是,这使用户可以删除用于呈现行号之前的::。
如果我必须写一个解决方案,我应该让::在不可编辑之前,它已经有了显示'inline-block‘,所以它应该可以工作,但是,我没有办法用CSS实现这一点。
代码:
.code-editor {
background-color: #ffffff;
border-radius: 0.25rem;
counter-reset: line;
font-family: monospace;
line-height: 0;
margin: 0 auto;
padding: 0.5rem;
width: 30rem;
}
.code-editor:focus {
outline: none;
}
.code-editor span {
display: block;
line-height: 1.5rem;
}
.code-editor span::before {
border-right: 1px solid #dddddd;
color: #888888;
content: counter(line);
counter-increment: line;
display: inline-block;
margin-right: 0.5rem;
padding-right: 0.5rem;
text-align: right;
width: 1.5rem;
}<pre class="code-editor" contenteditable>
<span>james</span>
</pre>
我希望编辑器能像现在一样工作,例如,它应该仍然为每个新行创建一个新的跨度。但是,我不希望用户能够编辑或删除之前可以在每个span中找到的::。
发布于 2019-02-04 19:04:12
您是否尝试在span上使用contenteditable属性而不是pre元素?
.code-editor {
background-color: #ffffff;
border-radius: 0.25rem;
counter-reset: line;
font-family: monospace;
line-height: 0;
margin: 0 auto;
padding: 0.5rem;
width: 30rem;
}
.code-editor:focus {
outline: none;
}
.code-editor span {
display: block;
line-height: 1.5rem;
}
.code-editor span::before {
border-right: 1px solid #dddddd;
color: #888888;
content: counter(line);
counter-increment: line;
display: inline-block;
margin-right: 0.5rem;
padding-right: 0.5rem;
text-align: right;
width: 1.5rem;
}<pre class="code-editor" >
<span contenteditable>james</span>
</pre>
https://stackoverflow.com/questions/54514374
复制相似问题