javascript - 我的 jquery 代码错误 `Uncaught TypeError: Cannot call method ' eq' of null`

标签 javascript jquery

请指出此代码中的错误。

$("#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 ,而是PrototypeMooTools (或完全是其他东西)。 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/

相关文章:

javascript - 无法理解函数的行为

javascript - 当我在 Chrome 控制台中使用 Angular.* 时会发生什么以及为什么它有效?

javascript - 如何使用 jQuery 和 CSS3 制作交互式侧边栏?

javascript - ipad portrait 的特定 JS 也在 landscape 中触发

javascript - 如何使用 Jquery REST API 在物联网上发送消息?

javascript - 页面上的多个表单,Ajax 在错误的表单上显示错误

javascript - jQuery 滚动到 ID 元素的底部

javascript - jQuery 拖动事件似乎没有 deltaX 属性

javascript - 使用Datatables导出动态表

javascript - jQuery 加载导致 Opera 浏览器加载整个页面