.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();
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<div id="vehicle-101031994" class="vehicle-year-profile">
<div class="left">
<h4>1994</h4>
</div>
<div class="right options">
<a class="edit" href="#"><img class="icon-small" src="/image/icons/pencil.png"></a>
<a class="delete" href="#"><img class="icon-small" src="/image/icons/delete.png"></a>
</div>
<div class="clear"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是因为
.parents() 和 .parent() 方法是相似的,除了后者只在 DOM 树上向上移动一层。
您正在寻找的父级高两个级别,因此您必须使用.parents()(甚至更好.closest())。
您引用的元素不是直接父元素。
试试这个:
that.remove(jQuery(this).closest('.vehicle-year-profile'));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8408 次 |
| 最近记录: |