IE11将剪贴板数据粘贴到输入元素烦恼 [英] IE11 pasting clipboard data to an input element annoyance

查看:361
本文介绍了IE11将剪贴板数据粘贴到输入元素烦恼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些JavaScript,基本上听取了特定输入文本元素上的粘贴事件。我通过jQuery做了这个事件绑定。现在,在以前的IE版本中,情况非常好。



但是升级到IE11后,行为会有所不同。



场景:
我正在复制和粘贴由CR和LF(电子表格中的多行)分隔的多个关键字。在JavaScript中,在粘贴事件处理程序中,我读取剪贴板数据并将所有CR / LF字符转换为单个逗号,然后将此转换后的数据字符串设置回剪贴板。再次,这在以前的IE版本中效果很好。



IE11中的行为如下所示:
在粘贴时,paste事件处理程序确实被调用和我提到的一切都发生了。不幸的是,输入元素中的粘贴结果仍然没有正确格式化。随后的粘贴会显示正确的逗号分隔字符串(因为我在处理程序中做的最后一件事是将转换后的字符串设置为剪贴板)。



问题:
我是否正确处理这个粘贴事件处理程序?
我再次处理粘贴事件,转换剪贴板数据,然后将转换的数据设置回剪贴板。在以前的IE版本中,这在第一次粘贴时有效。但在IE11中,转换后的数据字符串会粘贴到第一张粘贴后的任何粘贴事件中。



以下是函数:

  $(input)。bind(paste,function(e){
//用于多行电子表格数据
//格式以便将新的行和回车转换为逗号。
var rawText = window.clipboardData.getData(text);
var delimitedText = rawText.replace(/ \r\\\
/ g,',');
window.clipboardData.setData(text,delimitedText); //这就是为什么后续的粘贴可以正常工作。
});

非常感谢。

解决方案

我通过从其他帖子中找到类似的问题来解决这个问题。
解决方案是使用setTimeout,超时时间为100 ms。
以这种方式设置可以使它像魅力一样工作。


I had some javascript that basically listened for the "paste" event on a particular input text element. I did this event binding via jQuery. Now, in previous versions of IE, things worked great.

But after upgrading to IE11, the behavior is different.

Scenario: I'm copying and pasting multiple keywords separated by a CR and LF (multiple rows in a spreadsheet). In javascript, within the "paste" event handler, I read the clipboard data and convert all the CR/LF characters into a single comma, THEN i set this converted data string back to the clipboard. Again, this worked great in previous versions of IE.

The behavior in IE11 is as follows: Upon pasting, the "paste" event handler does get called and everything that I mentioned does happen. Unfortunately, the pasted result in the input element is still not formatted correctly. Subsequent pasting does show the correct comma-delimited string (because the last thing i do in the handler is set the converted string to the clipboard).

Question: Am i handling this paste event handler incorrectly? Again, I'm handling the paste event, converting the clipboard data, then setting the converted data back to the clipboard. In previous IE versions this worked upon first paste. But in IE11 the converted data string is pasted on any paste event AFTER the 1st paste.

Here is the function:

 $("input").bind("paste", function (e) {
       // for multi rows of spreadsheet data.
       // format so that new line & carriage return are converted into a comma.
       var rawText = window.clipboardData.getData("text");
       var delimitedText = rawText.replace(/\r\n/g, ', ');
       window.clipboardData.setData("text", delimitedText); // this is why subsequent pasting works fine.
});

Much appreciated.

解决方案

I resolved this, by finding similar questions from other posts. The solution was the using a setTimeout, with a timeout of 100 ms. Setting this way makes it work like a charm.

这篇关于IE11将剪贴板数据粘贴到输入元素烦恼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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