gpt4 book ai didi

jquery - 是什么导致了 jQuery 本身内部随机出现的错误?

转载 作者:数据小太阳 更新时间:2023-10-29 06:07:54 25 4
gpt4 key购买 nike

我很反对这个。每 5-10 次我重新加载我的应用程序,我就会收到一个源自 jQuery 内部的奇怪错误。我尝试在 jQuery 的非压缩版本中添加断点以尝试从错误中退出,但 Chrome 永远不会为它们停止,而是总是跳到错误处。

我使用的是 jQuery 1.7.2,但是这个错误也发生在 1.7 和 1.6.1 版本中。

在我的 javascript 应用程序中,我调用这行代码:

this.element.find('.banner-btn')

其中 this.element 是一个 jQuery 对象。没有理由这会导致问题,10 次中有 9 次没有问题。但是随机出现一些问题,然后每次调用该行代码时,结果都是此堆栈跟踪的某种变体:

Uncaught TypeError: Cannot call method 'apply' of undefined
makeArrayjquery-1.7.2.js:4858
Sizzlejquery-1.7.2.js:5110
jQuery.fn.extend.findjquery-1.7.2.js:5432

事实上,在我调用 $.fn.find 的中断之后任何我都会得到这个错误。我已经尝试过各种各样的事情,我一直在阅读 jQuery 源代码,但我对此一无所知。任何人都知道在哪里可以确定这一点?

更新:进一步深入兔子洞

我已将其深入到 jQuery 库中,并找到了错误首先出现的位置。有点。

为了跟踪 DOM 遍历,jQuery 使用 pushStack方法。这是在 jQuery.fn.find 方法中调用的,应该返回一个 jQuery 对象。它通常会这样做——但是当 jQuery 内部出现问题时,此函数只返回一个常规对象,没有 jQuery 的好处。这会导致错误。

好的,这里是 pushStack 的第一个源码,定义在 jquery-1.7.2.js 的第 241 行:

// Take an array of elements and push it onto the stack
// (returning the new matched element set)
pushStack: function( elems, name, selector ) {
if(window._debug_jQuery_pushStack) { debugger }
// Build a new jQuery matched element set
var ret = this.constructor();

// ---- the function continues, but this is enough for us ----

var ret = this.constructor(); 是应该添加 jQuery 好东西的地方。在这种情况下,他们不是。 this 以某种方式被破坏为常规对象,而不是 jQuery。所以现在我的问题是:我如何找到这种腐败的原因?困难在于,这种情况只会在我的应用程序加载 10 次或 20 次时发生一次,而且看起来完全是随机的。

最佳答案

问题不在于您的代码,而在于 Chrome 本身——参见 bug #125148 .当前的解决方法是按如下方式更改 pushStack:

pushStack: function( elems, name, selector ) {
// Build a new jQuery matched element set
var ret = this.constructor();

// temporary workaround for chrome issue #125148
// http://code.google.com/p/chromium/issues/detail?id=125148
if (!(ret instanceof jQuery.fn.init)) {
console.log("applying pushStack fix");
ret = new jQuery.fn.init();
}

// ... snip ...

另请参阅 this related问题。

关于jquery - 是什么导致了 jQuery 本身内部随机出现的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10251272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com