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

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

jQuery $(this).find() and $(selector,this) not working

javascriptjquerythis

提问by Meshaal

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

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

$('.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.

应该做同样的事情:选择第一个20(或19?).cell■在每个.chamber,并让他们红了。

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?

我究竟做错了什么?

采纳答案by Frédéric Hamidi

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 googlecodeand I cannot find any YNamevariable in there.

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

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.

我会在这里GetElementsByName()冒昧地说一下,由于某种原因,呼叫出现了乱码。尝试使用脚本的新副本。

回答by Emil Condrea

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

看这个例子:它运行良好http://jsfiddle.net/FCFmy/