我有 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/