HTML : How to modify multi-line copied text in IE before pasting it in a text field

天大地大妈咪最大 提交于 2019-12-12 06:03:57

问题


When we copy some text separated by new line and try to paste it in a text field in IE, the text gets truncated starting from the first new line character. In Firefox and Chrome, the new line character simply converts to a space. I am required to implement a uniform behaviour for all of these browsers.

If I can get hold of the copied text, I can convert all \r\n to \n and then all \n to the space character, but I can't think of a way to access the copied text before its truncated


回答1:


For IE only!! You can get the information from window.clipboardData as shown here:

$('input:text').on('paste', function(ev) {
    var multiCellTarget = $(this);
    setTimeout(function() {
        var cells = window.clipboardData.getData('text').split(/\s+/);
        if (cells.length > 1) {
            // [... do stuff with cells]
        }
    }, 20);
});

The 20 ms timeout is a trick to work around the problem that the clipboard data does not seem to be available at the time jquery kicks off the paste event.



来源:https://stackoverflow.com/questions/8884115/html-how-to-modify-multi-line-copied-text-in-ie-before-pasting-it-in-a-text-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!