【问题标题】:"Unable to get property 'getData' of undefined or null reference" in IE but not Chrome“无法在 IE 但不是 Chrome 中获取未定义或空引用的属性 'getData'”
【发布时间】:2015-02-28 13:09:58
【问题描述】:

感谢另一位会员的帮助,我成功实现了一个 JS 方法,可以粘贴 excel 数据并将其拆分为 HTML 文本框表格形式 (see thread)。

我现在面临的问题是,这仅在 Chrome 中有效,而 IE10 和 IE11 都标记以下错误:

“无法获取未定义或空引用的属性 'getData'。”

在函数的第 2 行(下)抛出此错误:

function (event) {
    var input_id = $(this).attr("id");
    var value = event.originalEvent.clipboardData.getData('text/plain'); //ERROR in IE
    /* ... */
    event.preventDefault(); // prevent the original paste
}

想知道是否有人能看到手头的问题,即为什么 Chrome 满意而 IE 不满意。

【问题讨论】:

标签: javascript excel google-chrome internet-explorer


【解决方案1】:

在这里找到答案:Intercept paste event in Javascript

这对我有用。

if (window.clipboardData && window.clipboardData.getData) { // IE
    pastedText = window.clipboardData.getData('Text');
}
else if (event.originalEvent.clipboardData && event.originalEvent.clipboardData.getData) { // other browsers
    pastedText = event.originalEvent.clipboardData.getData('text/plain');
}

【讨论】:

  • 这实际上是这个问题的正确答案,感谢 pj2452,看起来事件处理程序中的剪贴板数据对于 Internet Explorer 来说是 null。
【解决方案2】:

In IE, it should be:

var value = event.originalEvent.clipboardData.getData("Text"); 

【讨论】:

  • 谢谢。这可以在 Chrome 中使用吗?想知道如何让它在两个浏览器中都运行。
  • 如果没有可以尝试添加浏览器检查验证
  • 抱歉,我该怎么做呢?
  • 事实证明,我将“text/plain”修改为“Text”,但在 IE 中仍然出现同样的错误...
猜你喜欢
  • 2013-07-09
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 2018-09-29
  • 2014-08-01
  • 2020-04-28
相关资源
最近更新 更多