jquery - 从 JQuery 对象数组中获取元素

标签 jquery

查看我的代码:

var ths = $("#my_table table th");
if (ths.length > 1) {
    var $th = ths[1]; // tried here plain 'var th' - still got error 
    th.find('#map_column_element_id'); // error here!
}

我得到了 JQuery 对象数组。然后我尝试将第二个元素作为 JQuery 对象进行管理。当我发出

th.find(...)

我得到TypeError: th.find is not a function。我做错了什么?

最佳答案

您将获得原生 JS DOM 元素,它没有 find() 方法。使用 eq(),或使用 $(ths[1]) 重新包装元素。

我会使用eq(),如下所示:

var ths = $("#my_table table th");
if (ths.length > 1) {
    var $th = ths.eq(1);
    $th.find('#map_column_element_id');
}

也同意Andrew在评论中的观点,ID是唯一的,并且不需要find()它,只需执行$('#map_column_element_id') 应该足够了。

关于jquery - 从 JQuery 对象数组中获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13993388/

相关文章:

javascript - 变量赋值会阻止删除项目吗?

javascript - 使用 AJAX 和 JavaScript 在点击时显示模式

javascript - 如何使用 jQuery 隐藏我的 div,然后通过按不同的 div 再次显示它?

javascript - AngularJS 用连续的单词高亮显示用户名

php - Twitter 如何实时更新其博客文章?

javascript - 获取应用于特定元素的所有 css 样式的列表

javascript - 更改整个 html 文档的 FontSize 的简单方法

javascript - 为什么 jquery-rails 没有加载?

javascript - 在图像悬停时显示标尺并缩放它

javascript - 是否可以将 iframe 的 src 属性设置为 "fake"?