我遇到了同样的问题,我发现https://www.sitepoint.com/6-jquery-cursor-functions/ 有解决方案。这里有 6 种方法可以让您在输入/文本区域中获取/设置光标位置。我相信这也适用于内容可编辑字段!
这非常有用,因为该错误仅针对 IE 和 Windows 7 组合显示。
这是我之前的代码
$body.on('input paste','.replace-special-chars',function () {
let coma = /‚/g;
let doubleQuotes = /[“”]/g;
let singleQuotes = /[‘’]/g;
$(this).val($(this).val().replace(doubleQuotes,'"'));
$(this).val($(this).val().replace(coma,','));
$(this).val($(this).val().replace(singleQuotes,"'"));
$(this).val($(this).val().replace(/[^\x00-\xff]/g, '- '));
});
以及我在上面提到的网站上找到的使用 jquery 方法的后续代码
$body.on('input paste','.replace-special-chars',function () {
let position = $(this).getCursorPosition();
let coma = /‚/g;
let doubleQuotes = /[“”]/g;
let singleQuotes = /[‘’]/g;
$(this).val($(this).val().replace(doubleQuotes,'"'));
$(this).val($(this).val().replace(coma,','));
$(this).val($(this).val().replace(singleQuotes,"'"));
$(this).val($(this).val().replace(/[^\x00-\xff]/g, '- '));
$(this).setCursorPosition(position);
});