我有contentEditable = 'true'的div标签。当我从键盘输入文本,然后调用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>
发布于 2015-08-19 23:47:02
在doUndo()中,不需要调用execCommand,只要最后一个字符不为空,就可以将其删除。
例如。
function doUNDO() {
var mainDIV = document.getElementById('mainDiv')
mainDIV.innerHTML = mainDIV.innerHTML.substr(0, mainDIV.innerHTML.length - 1);}
https://stackoverflow.com/questions/32100027
复制相似问题