jQuery $(this).find() 和 $(selector,this) 不起作用

发布于 2024-11-27 05:54:22 字数 1878 浏览 1 评论 0原文

我无法让 jQuery 的 $(this) 函数按预期工作......或者更确切地说,根本无法工作。据我了解,

$('.chamber').each(function(){
    $(this).find('.cell').slice(0,19).css('background-color','red');
});

$('.chamber').each(function(){
    $('.cell',this).slice(0,19).css('background-color','red');
});

都应该做同样的事情:选择每个 .chamber 中的前 20 个(或 19?).cell,并将它们设为红色。

不幸的是,实际发生的情况是他们都抛出相同的错误:

Uncaught exception: ReferenceError: Undefined variable: YName
Error thrown at line 19, column 8870 in <anonymous function: CLASS>(W, U, V, T, Z, aa) in jquery-1.3.2.min.js:
    if(Z^(YName&&(" "+YName+" ").indexOf(W)>=0))
called from line 19, column 3944 in <anonymous function: F.filter>(ad, ac, ag, W) in jquery-1.3.2.min.js:
    Y=I.preFilter[ab](Y,aa,ag,ai,W,Z);
called from line 19, column 896 in <anonymous function>(Y, U, ab, ac) in jquery-1.3.2.min.js:
    af=F.filter(ae.expr,ae.set);
called from line 19, column 21013 in <anonymous function: F>(Y, X, V, W) in jquery-1.3.2.min.js:
    return T(Y,X,V,W)
called from line 12, column 5629 in <anonymous function: find>(E) in jquery-1.3.2.min.js:
    o.find(E,this[0],F);
called from line 169, column 3 in <anonymous function>():
    $(this).find('.cell').slice(0,19).css('background-color','red');
called via Function.prototype.call() from unknown location in <anonymous function: each>(G, K, F) in jquery-1.3.2.min.js:
    /* no source available */
called from line 12, column 2276 in <anonymous function: each>(F, E) in jquery-1.3.2.min.js:
    return o.each(this,F,E)
called from line 168, column 2 in <anonymous function>():
    $('.chamber').each(function(){
called from line 26, column 460 in <anonymous function: ready>():
    a.call(s,c);

我做错了什么?

I'm having trouble getting jQuery's $(this) function to work as expected... or rather, to work at all. As I understand it, both

$('.chamber').each(function(){
    $(this).find('.cell').slice(0,19).css('background-color','red');
});

and

$('.chamber').each(function(){
    $('.cell',this).slice(0,19).css('background-color','red');
});

should do the same thing: select the first 20 (or 19?) .cells in each .chamber, and make them red.

Unfortunately, what actually happens is they both throw the same error:

Uncaught exception: ReferenceError: Undefined variable: YName
Error thrown at line 19, column 8870 in <anonymous function: CLASS>(W, U, V, T, Z, aa) in jquery-1.3.2.min.js:
    if(Z^(YName&&(" "+YName+" ").indexOf(W)>=0))
called from line 19, column 3944 in <anonymous function: F.filter>(ad, ac, ag, W) in jquery-1.3.2.min.js:
    Y=I.preFilter[ab](Y,aa,ag,ai,W,Z);
called from line 19, column 896 in <anonymous function>(Y, U, ab, ac) in jquery-1.3.2.min.js:
    af=F.filter(ae.expr,ae.set);
called from line 19, column 21013 in <anonymous function: F>(Y, X, V, W) in jquery-1.3.2.min.js:
    return T(Y,X,V,W)
called from line 12, column 5629 in <anonymous function: find>(E) in jquery-1.3.2.min.js:
    o.find(E,this[0],F);
called from line 169, column 3 in <anonymous function>():
    $(this).find('.cell').slice(0,19).css('background-color','red');
called via Function.prototype.call() from unknown location in <anonymous function: each>(G, K, F) in jquery-1.3.2.min.js:
    /* no source available */
called from line 12, column 2276 in <anonymous function: each>(F, E) in jquery-1.3.2.min.js:
    return o.each(this,F,E)
called from line 168, column 2 in <anonymous function>():
    $('.chamber').each(function(){
called from line 26, column 460 in <anonymous function: ready>():
    a.call(s,c);

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2024-12-04 05:54:22

首先,你是对的,两种语法是等效的。

您的缩小版 jQuery 脚本似乎有问题。我从 googlecode 下载了相同的文件,但找不到任何 < code>YName 变量在那里。

我在这里冒险说,由于某种原因,对 GetElementsByName() 的调用出现了乱码。尝试使用脚本的新副本。

First, you're correct, both syntaxes are equivalent.

There seems to be a problem with your minified jQuery script. I downloaded the same one from googlecode and I cannot find any YName variable in there.

I'll go out on a limb here and say calls to GetElementsByName() have been garbled for some reason. Try using a fresh copy of the script.

眼眸里的快感 2024-12-04 05:54:22

看这个例子:它工作得很好 http://jsfiddle.net/FCFmy/

see this example : it works well http://jsfiddle.net/FCFmy/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文