开发者

jQuery .parent() does not work

Why the following code fails with:

Error: class_a_jquery_objects[0].开发者_如何学运维parent is not a function

?

HTML:

<div>
    <div class='a b'></div>
    <div class='b c'></div>
    <div class='c a'></div>
</div>    
<div id='log'></div>

JS:

$(function() {
    var class_a_jquery_objects = $(".a");

    $("#log").append(class_a_jquery_objects.length + "<br />");
    $("#log").append(class_a_jquery_objects[0] + "<br />");
    $("#log").append(class_a_jquery_objects[0].parent() + "<br />");
});


class_a_jquery_objects[0] is a DOM element and not a jQuery object. You can't call jQuery methods with it. You need to first wrap it in a jQuery object:

$(class_a_jquery_objects[0]).parent()


You need to wrap it with JQuery object

   $("#log").append($(class_a_jquery_objects[0]).parent() + "<br />");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜