简体   繁体   English

contenteditable div退格键和删除文本节点问题

[英]contenteditable div backspace and deleting text node problems

There are so many issues with contenteditable divs and deleting html and/or non content editable content inside editable divs. contenteditable div以及删除可编辑div中的html和/或非内容可编辑内容有很多问题。

Using an answer by the excellent Tim Down here: How to delete an HTML element inside a div with attribute contentEditable? 在此处使用出色的Tim Down的答案: 如何删除具有contentEditable属性的div中的HTML元素?

Using Tim's code, the entire text node gets deleted. 使用Tim的代码,将删除整个文本节点。 I need this to work like any textarea would, deleting character by character and just making sure html elements can be backspaced as well. 我需要像任何textarea一样工作,逐字符删除字符,只是确保html元素也可以退格。

I tried the following 我尝试了以下

else if(node){
var index = node.length-1;
if(index >= 0)
node.deleteData(index,1);
else
this.removeChild(node);
}

But this is obviously not going to work correctly. 但这显然不能正常工作。 If I am at the end of the content, things work as expected. 如果我在内容结尾处,则说明一切正常。 But if I place the cursor anywhere else, it's still deleting from the end. 但是,如果我将光标放在其他任何地方,它仍将从末尾删除。

I'm lost at this point, any help is very appreciated 我现在迷路了,非常感谢您的帮助

http://jsfiddle.net/mstefanko/DvhGd/1/ http://jsfiddle.net/mstefanko/DvhGd/1/

After breaking down how google uses contenteditable divs in their google plus user tagging, I landed on a much more reasonable solution. 在分解了Google如何在其Google加用户标记中使用contenteditable div的方法之后,我找到了一个更合理的解决方案。 Maybe it will help someone else out. 也许它将帮助其他人。

Google Plus帖子小部件

After adding 1 tag, you can already see a lot of differences in the html browser to browser. 添加1个标记后,您已经可以看到html浏览器与浏览器之间的许多差异。

谷歌浏览器源

In Google Chrome, a space is added with each tag. 在Google Chrome浏览器中,每个标记都添加了一个空格。 The button tag is used. 使用了按钮标签。 And the chrome-only contenteditable="plaintext-only" is used. 并且使用仅Chrome的contenteditable =“ plaintext-only”。

Google Chroem来源

When I backspace the space in chrome, a BR tag is then appended. 当我在chrome中退格时,然后会附加一个BR标签。

在此处输入图片说明

In Firefox the BR tag is added immediately with the first tag. 在Firefox中,BR标签会立即与第一个标签一起添加。 No spaces are needed. 不需要空格。 And an input tag is used instead of the button tag. 并且使用输入标签代替按钮标签。

The BR tag was the single greatest break-through I had while digging through this. BR标签是我在研究该标签时获得的最大突破。 Before adding this, there was a lot of quirky behavior with deleting tags, as well as focus issues. 在添加此代码之前,删除标签和焦点问题存在很多古怪的行为。

在此处输入图片说明

In IE, more interesting changes were made. 在IE中,进行了更有趣的更改。 A span with contenteditable false is used for the tags here. 此处将具有contenteditable false的范围用于标签。 No spaces or BR tags, but an empty text node. 没有空格或BR标签,但文本节点为空。

With all of that, you don't have to copy google exactly. 有了这些,您不必完全复制Google。

The important parts: 重要部分:

If you're rendering HTML, do the following... 如果要呈现HTML,请执行以下操作...

1. Chrome should use the button tag 1. Chrome应该使用按钮标记

2. Firefox/IE should use the input tag 2. Firefox / IE应该使用输入标签

For range/selection you generally want to treat things like tags as a single character. 对于范围/选择,您通常希望将诸如标签之类的内容视为单个字符。 You can build this into your range/selection logic, but the behavior of the input/button tags is much more consistent, and way less code. 您可以将其内置到范围/选择逻辑中,但是输入/按钮标签的行为更加一致,并且代码更少。

IE behaves better in IE7-8 using a span. IE在使用跨度的IE7-8中表现更好。 Just from a UI standpoint. 仅从UI的角度来看。 But if you don't care if your site is pretty in old versions of IE, the input has the correct behaviour in IE as well as firefox. 但是,如果您不在乎您的网站是否是IE的旧版本,则输入以及Firefox和Firefox中的行为都是正确的。

3. Chrome only, use the contenteditable="plaintext-only" attribute on your editable div. 3.仅适用于Chrome浏览器,请在可编辑div上使用contenteditable =“ plaintext-only”属性。

Otherwise, a lot of weird issues happen not only when a user tries to paste rich-text, but also when deleting html elements sometimes the styles can get transferred to the div, I noted many strange issues with this. 否则,不仅在用户尝试粘贴富文本格式时,而且在删除html元素时,有时还会将样式转移到div时,都会发生很多奇怪的问题,我注意到了许多奇怪的问题。

4. If you need to set the caret position to the end of the div, set the end of the range before the BR. 4.如果需要将插入符号位置设置为div的末尾,请将范围的末尾设置为BR。

for FireFox: 对于FireFox:

range.setEndBefore($(el).find('br')[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM