[英]How to I prevent deletion of contenteditable span?
I'm trying to use a contenteditable
span
.我正在尝试使用
contenteditable
span
。 But if, as the user, I delete all the text within the span, the span disappears and is no longer editable (still in the DOM though).但是,如果作为用户,我删除了跨度内的所有文本,跨度就会消失并且不再可编辑(尽管仍在 DOM 中)。 How do I prevent this?
我该如何防止这种情况?
I'm seeing this problem in both Chrome and FF.我在 Chrome 和 FF 中都看到了这个问题。
Here's what I've tried:这是我尝试过的:
<div class="subgroup" contenteditable="false" style="border: 1px solid red;"> <span class="header editable" contenteditable="true">initial contents</span> </div>
I put contenteditable="false"
on the parent element based on this answer from 2015 , but the result is the same whether or not that attribute is present on the parent.我根据2015 的这个答案将
contenteditable="false"
放在父元素上,但无论该属性是否存在于父元素上,结果都是相同的。
Of course, I could have JS intercept keypresses to prevent the user from completely clearing the contents, but that's a bad UX.当然,我可以让 JS 拦截按键以防止用户完全清除内容,但这是一个糟糕的用户体验。 I'd much prefer a pure HTML solution in any event.
无论如何,我更喜欢纯 HTML 解决方案。
In writing up this question, I discovered a solution.在写下这个问题时,我发现了一个解决方案。 For others who might need it: put a border on the
span
.对于可能需要它的其他人:在
span
上设置边框。
But there, as suggested by @G-Cyrillus, a better solution is to add padding to the span
when the node is empty:但是,正如@G-Cyrillus 所建议的那样,更好的解决方案是在节点为空时向
span
添加填充:
<style>.subgroup span:empty { padding-right:1em;} </style> <div class="subgroup" contenteditable="false" style="border: 1px solid red;"> <span class="header editable" contenteditable="true">initial contents</span> </div>
Anyone have a better solution?有人有更好的解决方案吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.