如何使用Javascript从粘贴事件中删除换行符?

时间:2018-10-03 13:42:16

标签: javascript internet-explorer paste strip

我目前有代码可以剥离粘贴上的所有格式,因此用户只是粘贴纯文本,而不能剥离换行符。我试过添加行text = text.replace(/(\ r \ n \ t | \ n | \ r \ t)/ gm,“”);删除换行符,但这不起作用。我需要此功能才能在IE 11中运行,该如何完成?

 $('[contenteditable]').on('paste', function (e) {
    e.preventDefault();
    var text = '';
    if (e.clipboardData || e.originalEvent.clipboardData) {
        text = (e.originalEvent || e).clipboardData.getData('text/plain');
    } else if (window.clipboardData) {
        text = window.clipboardData.getData('Text');
    }
    if (document.queryCommandSupported('insertText')) {
        document.execCommand('insertText', false, text);
    } else {
        document.execCommand('paste', false, text);
    }
});

0 个答案:

没有答案