jQuery.parent()似乎不起作用

.parent()不返回我指定的父元素。 我没有看到我的代码或HTML的任何问题。

使用Javascript:

var vehicle = function () { return { init: function () { var that = this; jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) { e.preventDefault(); console.log(e.currentTarget); // [a.close #] that.remove(jQuery(e.currentTarget).parent('.vehicle-year-profile')); }); }, remove: function (el) { console.log(el); // [] jQuery(el).remove(); } } }(); jQuery(function() { vehicle.init(); }); 

HTML:

  

1994

这是因为

.parents()和.parent()方法类似,只是后者只在DOM树上运行一个级别。

你正在寻找的父级是两级,所以你必须使用.parents() (甚至更好.closest() )。

您引用的元素不是直接父级。

试试这个:

 that.remove(jQuery(this).closest('.vehicle-year-profile'));