javascript 在 Internet Explorer 上粘贴事件侦听器获取错误参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30125770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Paste event listener on Internet Explorer getting wrong arguments
提问by Victor
I'm handling the paste events for a contenteditable
to clean all HTML markers before paste. All Works fine in Firefox
and Chrome
. But when I test my code in IE11
, the event object
passed is not a ClipboardEvent
but a DragEvent
.
我正在处理粘贴事件以contenteditable
在粘贴之前清除所有 HTML 标记。在Firefox
和 中一切正常Chrome
。但是当我测试我的代码时IE11
,event object
通过的不是 aClipboardEvent
而是 a DragEvent
。
Is there something wrong with my code? If I add the listener as the code bellow, should I get the clipboard event. Why I'm getting drag?
我的代码有问题吗?如果我将侦听器添加为下面的代码,我是否应该获得剪贴板事件。为什么我被拖了?
editable.addEventListener('paste', pasteHandler, false);
http://jsfiddle.net/vepo/4t2ofv8n/
http://jsfiddle.net/vepo/4t2ofv8n/
To test the example above, I'm copy a text from Chrome and paste into IE. But I you copy any text from IE will get the same error.
为了测试上面的例子,我从 Chrome 复制了一个文本并粘贴到 IE 中。但是我从 IE 复制任何文本都会得到同样的错误。
回答by william.eyidi
EDIT
编辑
$(document).ready(function(){
var editable = document.getElementById('editable-div');
var pasteHandler = function(e){
if(e.clipboardData && e.clipboardData.getData) {
var pastedText = "";
if (window.clipboardData && window.clipboardData.getData) { // IE
pastedText = window.clipboardData.getData('Text');
} else if (e.clipboardData && e.clipboardData.getData) {
pastedText = e.clipboardData.getData('text/plain');
}
alert(pastedText);
}
else{
alert('Not paste object!');
}
};
editable.addEventListener('paste', pasteHandler, false);
});
here I handle the IE Version and the other browsers as well.
在这里,我也处理 IE 版本和其他浏览器。
回答by codeMonkey
e.clipboardData
was always null for me on IE, so I came up with this:
e.clipboardData
在 IE 上对我来说总是 null,所以我想出了这个:
var pastedText = '';
if (typeof e.clipboardData === 'undefined')
pastedText = window.clipboardData.getData('Text')
else
pastedText = e.clipboardData.getData('text/plain')
回答by PetrolHead
e.originalEvent.clipboardData.getData('text/plain')
works for safari, chrome, firefox and safari and chrome on an Ipad.
e.originalEvent.clipboardData.getData('text/plain')
适用于 iPad 上的 safari、chrome、firefox 和 safari 和 chrome。
window.clipboardData.getData('text')
works for Internet Explorer and Edge.
window.clipboardData.getData('text')
适用于 Internet Explorer 和 Edge。
Note: e.originalEvent.clipboardData.getData('text')
works for desktop browsers but not for mobile browsers.
注意:e.originalEvent.clipboardData.getData('text')
适用于桌面浏览器,但不适用于移动浏览器。
So in the end I used this
所以最后我用了这个
var clipText;
if (e.originalEvent.clipboardData !== undefined){
clipText = e.originalEvent.clipboardData.getData('text/plain')
} else {
clipText = window.clipboardData.getData('text')
}
回答by sherin
$("element").on('paste', function (e)
{
if (window.clipboardData)
{
pastedText = window.clipboardData.getData('Text')
}
else if (e.clipboardData || e.originalEvent.clipboardData != undefined)
{
pastedText = e.originalEvent.clipboardData.getData('text/plain')
}
}
});