如何删除具有 contentEditable 属性的 div 内的 HTML 元素?
有这个html:
<div id="editable" contentEditable="true" >
<span contentEditable="false" >Text to delete</span>
</div>
需要用一个退格键删除跨度(以及里面的所有文本),这可能吗?
have this html:
<div id="editable" contentEditable="true" >
<span contentEditable="false" >Text to delete</span>
</div>
need that the span (and all text inside) is removed with a single backspace, is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明这比我想象的要复杂。或者我让它变得比需要的更复杂。无论如何,这应该适用于所有大型浏览器:
This turned out to be more complicated than I thought. Or I've made it more complicated than it needs to be. Anyway, this should work in all the big browsers:
因为您想要删除整个元素,所以最好将其设置为
contenteditable="false"
,这样浏览器就不会删除元素的内容。然后您可以使用此属性在事件处理程序中进行测试,如下所示:
jsfiddle 上的演示
Because you want to delete the whole element, it's better to make it
contenteditable="false"
so that browser won't let the contents of an element to be deleted.Then you can use this attribute for tests in event handler as follows:
demo on jsfiddle