简体   繁体   English

document explorer替代document.execCommand(“insertText”,...),用于文本插入,可由用户撤消/重做

[英]internet explorer alternative to document.execCommand(“insertText”,…), for text insertion that can be undone/redone by the user

When the user edits a contenteditable div , and press some keys, I would like to override the default behavior. 当用户编辑一个contenteditable div ,并按下某些键时,我想覆盖默认行为。 For instance, I want to insert a normal line break when the user press ENTER. 例如,我想在用户按ENTER时插入一个正常的换行符。 I do that using document.execCommand("insertText",...) 我使用document.execCommand("insertText",...)来做到这一点

This is the only way I have found so far to make this action undoable and redoable by the user. 到目前为止,这是我发现的唯一方法,使用户可以撤消重做此操作。

<div id="editor" contenteditable="true" style="white-space:pre-wrap">
Some text....
</div>

<script>
$("#editor").keydown(function(evt){
    console.log(evt.keyCode);
    if(evt.keyCode==13){
        document.execCommand("insertText",false,"\n");
        evt.preventDefault();
        evt.stopPropagation();
    }
}
</script>

This code works well on chrome and firefox. 这段代码适用于chrome和firefox。 But, ie does not support "inserttext". 但是,即不支持“inserttext”。 Would there be a way to insert text with ie, such that the user can undo it? 是否有办法插入文本,即用户可以撤消它?

IE 11 has new ms-beginUndoUnit and ms-endUndoUnit commands. IE 11具有新的ms-beginUndoUnitms-endUndoUnit命令。

I tried and failed to create a working solution for IE 11 using these. 我试过并且没能使用这些为IE 11创建工作解决方案。 Inserting a line break is hard with DOM ranges and selections; DOM范围和选择很难插入换行符; you can do it more easily in IE using its legacy document.selection and TextRange objects but unfortunately IE 11 removed document.selection (but not TextRange, strangely) which makes it difficult. 您可以使用其遗留的document.selectionTextRange对象在IE中更轻松地完成它,但不幸的是IE 11删除了document.selection (但不是TextRange,奇怪的是)这使得它变得困难。 After coding a nasty workaround to create a TextRange from the selection, undo didn't work anyway. 在编写了令人讨厌的变通方法以从选择中创建TextRange之后,撤消无论如何都不起作用。 Here's what I did: 这是我做的:

http://jsfiddle.net/E7sBD/2 http://jsfiddle.net/E7sBD/2

I decided to have another go using DOM Range and selection objects. 我决定再使用DOM Range和选择对象。 The line break insertion code is illustrative and shouldn't be used for anything serious because it involves adding a non-breaking space to give the caret somewhere to go (trying to place it directly after the <br> does not work). 换行插入代码是说明性的,不应该用于任何严重的事情,因为它涉及添加一个不间断的空间,以便将插入符号放在某个地方(尝试在<br>不起作用后直接放置它)。 Undoing does remove the inserted content but unfortunately doesn't move the caret. 撤消会删除插入的内容,但遗憾的是不会移动插入符号。

http://jsfiddle.net/E7sBD/4/ http://jsfiddle.net/E7sBD/4/

You can always go the simpler and more stable way to catch the change event on the input div and change the last char from the input string to your liking. 您始终可以使用更简单,更稳定的方式捕获输入div上的change事件,并根据您的喜好更改输入字符串中的最后一个char。

http://api.jquery.com/change is invented i think for that purpose :) http://api.jquery.com/change发明我想为此目的:)

Change your angel and you see a whole new world :3 改变你的天使,你会看到一个全新的世界:3

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

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