查看我的代码:
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/