javascript - 撤消 div 标记

标签 javascript html

我有 div 标签和 contentEditable = 'true'。当我从键盘输入文本然后调用 undo 函数 (document.execCommand('undo',false, null)) 时,我输入的所有文本都被删除.我只想撤消最后输入的字符。我该怎么做?

function doUNDO() 
{
    document.execCommand('undo',false, null);
}
<!DOCTYPE html>
<html>
    <head>
        <script src="formulas.js"></script>
        <link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
    </head>
    <body id="main" spellcheck="false"> 
        <span contenteditable="false">
            <button onclick="doUNDO()">UNDO</button>
        </span> 

        <div id = "mainDiv"contenteditable="true">a</div>  
    </body>
</html>

最佳答案

doUndo() 中而不是调用 execCommand 您可以只从 div 中删除最后一个字符,如果它不为空的话。

例如

function doUNDO() {
    var mainDIV = document.getElementById('mainDiv') 
    mainDIV.innerHTML = mainDIV.innerHTML.substr(0, mainDIV.innerHTML.length - 1);

关于javascript - 撤消 div 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32100027/

相关文章:

javascript - 防止在文本框中打点

jquery - bootstrap 的分段按钮下拉列表在 firefox 27.0.1 中不起作用

javascript - 当 React 元素的文本更改时呈现 CSS 过渡

javascript - React axios 多文件上传

javascript - ngFor 中的 Angular 6 ngModel 不起作用

html - 将文本换行到 div 内的新行

php - 我的 CSS 代码在 TinyMCE 中没有正确读取

javascript - 假路径Javascript问题

javascript - Ionic 和 Angularjs - 表单绑定(bind)似乎只是一种方式。

javascript - 从 .catch(error) 返回未在 Node/Express 中返回