jQuery 中的 IE Javascript 错误“对象不支持此属性或方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3968416/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:54:46  来源:igfitidea点击:

IE Javascript error "Object doesn't support this property or method" within jQuery

javascriptjqueryinternet-explorersizzle

提问by Aistina

For some reason, I am getting the following Javascript error in Internet Explorer 8 on line 3156 of jquery.js (version 1.4.3, non-compressed version): Object doesn't support this property or method. No error occurs in Firefox and Google Chrome.

出于某种原因,我正在上的jquery.js(版本1.4.3,非压缩版本)的3156线在Internet Explorer 8以下JavaScript错误:Object doesn't support this property or method。在 Firefox 和 Google Chrome 中没有发生错误。

This is the line the error occurs on:

这是发生错误的行:

if ( (match = Expr.leftMatch[ type ].exec( expr )) != null && match[2] ) {

Investigation (console.log(Expr.leftMatch[type])) produces the following interesting result: In Google Chrome, it outputs

调查 ( console.log(Expr.leftMatch[type])) 产生以下有趣的结果:在 Google Chrome 中,它输出

/(^(?:.|\r|\n)*?):((?:[\w\u00c0-\uFFFF\-]|\.)+)(?:\((['"]?)((?:\([^\)]+\)|[^\(\)]*)+)\))?(?![^\[]*\])(?![^\(]*\))/

However in Internet Explorer this is the output:

但是在 Internet Explorer 中,这是输出:

function() {
  var p = this;
  do p = p.previousSibling;
  while (p && p.nodeType != 1);
  return p;
}

On which execcannot be called (it is undefined). The quoted function is not present within jquery.js. Does anyone have any clue why this happens, or what I can do to solve it?

在这exec不能叫(这是不确定的)。引用的函数在 jquery.js 中不存在。有没有人知道为什么会发生这种情况,或者我能做些什么来解决它?

I have, unfortunately, not yet been able to create a simple script to reproduce the problem, although I did find this post of someone having the same problem, but it does not offer a solution (the last post suggests the page should be run in Standards Mode, but mine already is).

不幸的是,我还没有能够创建一个简单的脚本来重现问题,虽然我确实找到了有同样问题的人的这篇文章,但它没有提供解决方案(最后一篇文章建议该页面应该在标准模式,但我的已经是)。

回答by Aistina

As it turns out, I managed to figure it out by myself after several painful hours. It appears the Sizzle selector engine breaks in this unexpected way (and only in Internet Explorer), if you have defined Object.prototype.previousObjectelsewhere.

事实证明,经过几个小时的痛苦之后,我自己设法弄明白了。如果您已Object.prototype.previousObject在别处定义,那么 Sizzle 选择器引擎似乎会以这种意想不到的方式中断(并且仅在 Internet Explorer 中)。

Removing that declaration, or renaming previousObjectto something else fixes the problem.

删除该声明或重命名previousObject为其他内容可以解决问题。

The funny thing is, I even put that code there myself (the Object.prototype.previousObject = [the function in my question]), but I did not recognize the code.

有趣的是,我什至自己把那个代码(the Object.prototype.previousObject = [the function in my question])放在那里,但我不认识这个代码。

Well, that's another day full of development potential wasted.

好吧,那又是充满浪费的开发潜力的一天。

回答by Malcolm

I have discovered the same behaviour occurs if you attempt to add a method called "inherited" to the Object.prototype, ie Object.prototype.inherited = <some func>

我发现如果您尝试向 Object.prototype 添加一个名为“继承”的方法,则会发生相同的行为,即 Object.prototype.inherited = <some func>

It affects IE6, 7 & 8 but seems to be fixed in IE9 (beta)

它会影响 IE6、7 和 8,但似乎已在 IE9(测试版)中修复