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

本小妞迷上赌 提交于 2019-12-22 09:38:12

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6882810/jquery-this-find-and-selector-this-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!