使一些关键词在内容可编辑的 div 中无法移除。

huangapple go评论67阅读模式
英文:

Make some keywords not removable in content editable div

问题

<!-- 开始片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-css -->

    span[contenteditable="false"] {
      color: red;
    }

<!-- 语言: lang-html -->

    <div contenteditable="true" id="editableDiv">
      可编辑的内容 <span contenteditable="false">不可编辑的内容</span> 可编辑的内容
    </div>

<!-- 结束片段 -->
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

span[contenteditable=&quot;false&quot;] {
  color: red;
}

<!-- language: lang-html -->

&lt;div contenteditable=&quot;true&quot; id=&quot;editableDiv&quot;&gt;
  Editable Span &lt;span contenteditable=&quot;false&quot;&gt;Not Editable Span&lt;/span&gt; Editable Span
&lt;/div&gt;

<!-- end snippet -->

Looking at this question, I am able to make some keywords not editable in content editable. However, when I go on editable part of the span and I press backspace or del button, I can remove the not editable span. I tried with some JS but I did not succeed to make it work. So the question is different because I would like to avoid to be able to delete the not editable span

答案1

得分: 1

我刚刚发现了contenteditable代码。也许你的意思是这样的:

<!-- 开始代码段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-css -->
span[contenteditable="false"] {
  color: red;
}
span[contenteditable="true"] {
  outline: none;
}

<!-- 语言:lang-html -->
<div id="editableDiv">
   <span contenteditable="true">可编辑的文本</span> 
   <span contenteditable="false">不可编辑的文本</span>     
   <span contenteditable="true">可编辑的文本</span> 
</div>

<!-- 结束代码段 -->
英文:

I just found out there is contenteditable code. maybe what you mean is like this

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

span[contenteditable=&quot;false&quot;] {
  color: red;
}

span[contenteditable="true"] {
outline: none
}

<!-- language: lang-html -->

&lt;div id=&quot;editableDiv&quot;&gt;
   &lt;span contenteditable=&quot;true&quot;&gt;Editable Span&lt;/span&gt; 
   &lt;span contenteditable=&quot;false&quot;&gt;Not Editable Span&lt;/span&gt;     
   &lt;span contenteditable=&quot;true&quot;&gt;Editable Span&lt;/span&gt; 
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月10日 16:19:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定