javascript execCommand ('delete' ) 不删除 chrome 中的所有选择 div

标签 javascript google-chrome contenteditable rangy execcommand

我尝试用 contenteditable & execCommand 编写一个编辑器 在 Firefox 上一切正常,但在 chrome 中“删除”命令出错。

请查看下面的照片:

enter image description here

这是我的代码:

var $obj = $('#myBlockDivId');
var selection = rangy.getSelection();
if (selection.rangeCount > 0) selection.removeAllRanges();
var range = rangy.createRange();
range.selectNode($obj[0]);
selection.addRange(range);
range.select();

当我控制台日志:rangy.getSelection().toHtml() ==> 是对的

但是当我打电话时:

document.execCommand("delete", null, false);

在 Firefox 上没问题,但在 Chrome 上就不行,包装器 div 没有被删除。

我该如何解决这个问题?我必须使用 execCommand,因为它支持撤消和重做功能。所以,我不能使用 jquery 或 javascript dom 选择器来删除 div。

(我的英语不好,有人请编辑我的问题更清楚,非常感谢)

最佳答案

也许你可以尝试做:
(单独注意参数)

<span contentEditable>
	Select something here and after click on delete.
</span>
<input type="button" value="Delete selection" onclick="document.execCommand('delete')"/>


根据 Enabled commands 上的 w3c.github.io :

At any given time, a supported command can be either enabled or not. Authors can tell whether a command is currently enabled using queryCommandEnabled(). Commands that are not enabled do nothing, as described in the definitions of the various methods that invoke commands.

一些有用的链接:

w3c 非官方草案 2015 年 10 月 15 日:

其他资源:

希望对您有所帮助!

关于javascript execCommand ('delete' ) 不删除 chrome 中的所有选择 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33429105/

相关文章:

javascript - 根据选择显示表格/表格

javascript - 在 jquery 循环中生成多个 div

javascript - Web Speech API 在商业应用中的使用

javascript - 在 Chrome 开发者工具中同时查看和暂停 JS、CSS 和 HTML

javascript - 如何在 map 功能中先跳过

javascript - false == (x > y) 是如何工作的?

html - 注入(inject)输入字段等的 pmbx_context 是什么

javascript - 通过在 contenteditable 上按 Enter 制作一个 <br> 而不是 <div></div>

javascript - ContentEditable 标志?

html - contentEditable DIV 上的框大小以隐藏第一个和最后一个字符