在使用.children for .modal工作后,为什么还需要另一个$()

jer*_*mey 0 javascript jquery twitter-bootstrap

我想知道为什么我必须这样做:

$($("body").children("div")[0]).modal('show');

代替

$("body").children("div")[0].modal('show');

该函数不会.children返回包含节点的列表吗?我是jquery的新手,而不是javascript经验,所以我只是想知道有什么区别.

.modal 是一个引导函数,万一有人想知道.

以下是完整性的上下文:

'click .edit': function (e, value, row, index) {
    $.ajax({
        url: "index.php?controller=" + controller + "&action=edit&id=" + row['id'] + "&type=modal",
        success: function (result) {
            if (result !== null) {
                $("body").prepend(result);
                $($("body").children("div")[0]).modal('show');
            } else {
                alert("There was a problem with editing. Please contact a system admin or try again.");
            }
        }
    });
    e.stopPropagation();
},
Run Code Online (Sandbox Code Playgroud)

Ano*_*shi 6

因为("div")[0]将返回一个dom元素.它没有关联的jquery函数.为了将jquery函数与该元素相关联,您需要通过变形将其再次转换为jquery元素$()

其他选择是使用这样的eq()运算符,

$("body").children("div:eq(0)").modal('show');