jQuery即on paste上的问题(带有break的内容) [英] jQuery ie issue with on paste (content with break)

查看:182
本文介绍了jQuery即on paste上的问题(带有break的内容)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在输入字段中粘贴内容(我必须使用输入字段)并将粘贴的内容粘贴到其他输入。我的内容如下(复制所有行和粘贴):

I want to paste a content in an input field (I have to use input field) and get the pasted content to other input. my content is like the following (copy all lines and paste):

1234
4567
4321

以下链接正常但IE

http://jsfiddle.net/5bNx4/42/

        $editor.on('paste', function() {
var $self = $(this);            
              setTimeout(function(){ 
                var $content = $self.val();             
                $clipboard.val($content);
            },100);
     });

使用IE时粘贴内容时,只有第一行(1234)会出现在第二个输入。但是其他浏览器可以获得所有内容。

when using IE and when paste the content, only the first line (1234) will be appear into the second input. but other browsers you get all the content.

任何人都可以帮助我吗
谢谢,

Can anyone help me out here Thanks,

推荐答案

< IE> IE无法处理新行,它只会粘贴在第一行并忽略其余行。 用空格替换换行符可以解决问题。

IE fails to handle new line and it will on only paste in the first line and disregard the rest. Replacing newlines characters with space does the trick.

clipped = clipped.replace(/(\r\n|\n|\r)/gm, " "); //replace newlines with spaces

要克服将以下代码添加到脚本中,这样可以正常工作。

To overcome add the below code to your script and this will work fine.

if (window.clipboardData) {
    $('#editor').bind('paste', function (e) {
        var clipped = window.clipboardData.getData('Text');
        clipped = clipped.replace(/(\r\n|\n|\r)/gm, " "); //replace newlines with spaces
        $(this).val(clipped);
        return false; //cancel the pasting event
    });
}

检查这个 JSFiddle

参考: 允许在IE文本框中粘贴多行

编辑:已移除 console.log

< a href =http://jsfiddle.net/praveen_jegan/5bNx4/46/>更新了JSFiddle

这篇关于jQuery即on paste上的问题(带有break的内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆