首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止用户在使用“contenteditable”时编辑或删除::之前的内容?

如何防止用户在使用“contenteditable”时编辑或删除::之前的内容?
EN

Stack Overflow用户
提问于 2019-02-04 18:37:28
回答 1查看 44关注 0票数 1

我正在尝试构建一个文本编辑器,在每一行之前显示一个行号。用户可以编辑内容,这要归功于“contenteditable”HTML属性。但是,这使用户可以删除用于呈现行号之前的::。

如果我必须写一个解决方案,我应该让::在不可编辑之前,它已经有了显示'inline-block‘,所以它应该可以工作,但是,我没有办法用CSS实现这一点。

代码:

代码语言:javascript
复制
.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;
}
代码语言:javascript
复制
<pre class="code-editor" contenteditable>
        <span>james</span>
    </pre>

我希望编辑器能像现在一样工作,例如,它应该仍然为每个新行创建一个新的跨度。但是,我不希望用户能够编辑或删除之前可以在每个span中找到的::。

EN

回答 1

Stack Overflow用户

发布于 2019-02-04 19:04:12

您是否尝试在span上使用contenteditable属性而不是pre元素?

代码语言:javascript
复制
.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;
}
代码语言:javascript
复制
<pre class="code-editor" >
  <span contenteditable>james</span>
</pre>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54514374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档