<p><code>$(this)</code>对命名的click回调函数不可用。使代码更显式的一种方法是存储<code>$(this)</code>,正如其他人所说的,或者简单地使用已经传递的<code>id</code>。例如:</p>
<pre><code>var $deleteClicked = function(event) {
var url = Urls.deleteTempImage(event.target.id);
$.ajax({
url: url,
data: {
'id': event.target.id
},
success: function (data) {
console.log('spam');
$("#"+event.target.id).closest('.image-preview').remove();
}
});
}
$(document).on('click', '.delete-temp-image-link', $deleteClicked);
</code></pre>
<p>另外,请注意,我使用jQuery <code>.closest()</code>而不是<code>.parents()</code>。从<a href="https://api.jquery.com/closest/" rel="nofollow noreferrer">jQuery docs</a>,<code>.closest()</code>执行以下操作:</p>
<blockquote>
<p>For each element in the set, get the first element that matches the
selector by testing the element itself and traversing up through its
ancestors in the DOM tree.</p>
</blockquote>
<p>查看docs页面,了解<code>.closest()</code>和<code>.parents()</code>之间差异的描述。主要区别在于<code>.closest()</code>只遍历DOM树直到找到匹配项,而不是遍历到根元素。我怀疑这会带来巨大的性能影响,但由于您只选择了一个<code><div></code>,所以它的代码要稍微精确一些。你知道吗</p>