jQuery .parent()不起作用 [英] jQuery .parent() does not work
本文介绍了jQuery .parent()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么以下代码失败,并显示以下信息:
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]是DOM元素,而不是jQuery对象.您不能使用它调用jQuery方法.您需要先将其包装在jQuery对象中:
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()
这篇关于jQuery .parent()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文