javascript - jQuery $(this).find() 和 $(selector,this) 不工作

标签 javascript jquery this

我无法让 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);

我做错了什么?

最佳答案

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

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

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

关于javascript - jQuery $(this).find() 和 $(selector,this) 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6882810/

相关文章:

javascript - 如何将这两行 javascript 行组合成一行

javascript - 对象不是jquery中的函数

javascript - 使用 JQuery 查找 body 标签下所有子级的宽度,直至特定代

php - $.post 返回 [object 对象]

javascript - 数字/字符串原型(prototype)中的 `this` 是什么?

javascript - Javascript 中的 .bind(this) 与 var context = this - 最好的风格是什么?

javascript - 如何数据绑定(bind) Angular-ui-bootstrap 轮播控件?

JavaScript 对象规则

jquery - Flexslider 幻灯片不会在 Firefox 和 Chrome Canary 上显示

javascript - this 和 javascript 中的闭包(在 paper.js 中工作)