我正在尝试创建一个HTML5contenteditable div,只接受纯文本。我在下面使用 html 和 jQuery:
HTML
<div contenteditable="true"></div>
jQuery
(function () {
$('[contenteditable]').on('paste', function (e) {
e.preventDefault();
var text = null;
text = (e.originalEvent || e).clipboardData.getData('text/plain') || prompt('Paste Your Text Here');
document.execCommand("insertText", false, text);
});
});
但它并不适用于所有浏览器。getData
Internet Explorer 浏览器不支持。我尝试了 stackoverflow 上提到的很多解决方案,但没有一个对我有用。
我也尝试过
(function () {
$('[contenteditable]').on('paste', function (e) {
e.preventDefault();
var text = null;
if (window.clipboardData && clipboardData.setData) {
text = window.clipboardData.getData('text');
} else {
text = (e.originalEvent || e).clipboardData.getData('text/plain') || prompt('Paste Your Text Here');
}
document.execCommand("insertText", false, text);
});
});
但在那种情况下document.execCommand("insertText", false, text);
不适用于 IE。
有什么方法可以在过滤 HTML 标签后接受数据,以便任何人通过类型、粘贴、删除或任何其他方式在可编辑 div 中输入数据。它应该将其显示为文本。