我尝试用 contenteditable & execCommand 编写一个编辑器 在 Firefox 上一切正常,但在 chrome 中“删除”命令出错。
请查看下面的照片:
这是我的代码:
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/