javascript execCommand('delete')不会删除Chrome中的所有选择div [英] javascript execCommand('delete') not delete all selection div in chrome

查看:402
本文介绍了javascript execCommand('delete')不会删除Chrome中的所有选择div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写一个具有contenteditable& execCommand 在Firefox上一切正常,但是在chrome中,删除"命令存在错误.

I try to write an editor with contenteditable & execCommand Everything works fine on Firefox, but in chrome has an error with 'delete' command.

请查看下面的照片:

这是我的代码:

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()==>是正确的

when i console log: rangy.getSelection().toHtml() ==> it's right

但是当我打电话时:

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

在Firefox上很好,但在Chrome上不合适,包装器div不会被删除.

it's fine on Firefox but not right on Chrome, the wrapper div is not being deleted.

我该如何解决?我必须使用execCommand,因为它支持撤消和重做功能.因此,我无法使用jquery或javascript dom选择器删除div.

How can i fix this? I have to use execCommand because it's support undo and redo function. so, i can't use jquery or javascript dom selector to remove div.

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

(I bad at English, someone please edit my question for more clearly, many thanks)

推荐答案

也许您可以尝试做:
(仅注意论点)

Maybe you can try to do :
(note the argument alone)

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

根据已启用的命令上的w3c.github.io :

Acording to w3c.github.io on Enabled commands :

在任何给定时间,可以启用或不启用受支持的命令.作者可以使用queryCommandEnabled()判断当前是否启用了命令. 未启用的命令不执行任何操作 ,如调用命令的各种方法的定义所述.

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日 :

w3c Unofficial Draft 15 October 2015:

  • w3c execCommand
  • w3c Deleting the selection
  • w3C The delete command

其他资源 :

Other ressources :

  • List of contenteditable Browser Inconsistencies
  • quirksmode.org test page

希望这对您有帮助!

这篇关于javascript execCommand('delete')不会删除Chrome中的所有选择div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆