请指出此代码中的错误。
$("#gridTable tr").eq(1).find('td').forEach( function(){//这里有一些代码});
我尝试了不同的选择器,但没有任何效果。
我也尝试仅使用特定行的 id,但出现同样的错误,消息如下:
Cannot call method find of null
.
开启:
$("#firstRow").find('td').forEach( function(){//some code here});
最佳答案
在每种情况下,错误都会告诉您 $("#gridTable tr")
返回 null
。
这表明您没有使用 jQuery ,而是Prototype或MooTools (或完全是其他东西)。 jQuery 的 $()
函数永远不会返回 null
,但 Prototype 和 MooTools 的 $()
函数会返回 null
,如果它们没有找到具有给定 ID 的元素。如果您使用 Prototype 或 MooTools,请注意 $()
不像 jQuery 那样采用选择器,它采用 id
。因此,您不会在其上使用 #
,并且无法像示例中那样传递后代组合器。 (Prototype 中与 jQuery 的 $
最接近的等效项是 $$
。)
另外,如果您使用 jQuery,则 jQuery 对象没有 forEach
;他们确实有each
这是相似的(但是迭代器函数的参数顺序不同)。
关于javascript - 我的 jquery 代码错误 `Uncaught TypeError: Cannot call method ' eq' of null`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21456118/