2

查看我的代码:

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。我究竟做错了什么?

4

1 回答 1

6

您将获得没有find()方法的本机 JS DOM 元素。使用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')就足够了。

于 2012-12-21T16:08:32.653 回答