.empty() 清除事件

.empty() clears the event

本文关键字:事件 清除 empty      更新时间:2023-09-26

我的目标是存在于具有类 b_edit_meta 的div 中的js-trigger-restoreb,例如:

<div class="b_edit_meta">
   <div class="js-trigger-restoreb">Click</div>
</div>

每当我的代码中有以下行时:

jQuery(this).closest("li").find(".b_edit_meta").empty();

它会停止函数的其余部分工作,可能是因为.empty()正在清除它。

有没有办法在不清除事件的情况下清空该div?

jQuery(document).ready(function() {
    jQuery('.js-trigger-restoreb').bind("mouseup", function() {
        jQuery(this).closest("li").find(".b_edit_meta").empty();
        // because of above line, nothing else works here
    });
});

在jsFiddle上测试完整的代码。

在那.empty()之后,您不会以任何其他方式使用this,除了引用jQuery(this).closest("li")...然后引用某些东西。

尝试在清空容器之前将该引用保存到li

jQuery(document).ready(function() {
    jQuery('.js-trigger-restoreb').bind("mouseup", function() {
        var closestLi = $(this).closest("li");
        // find the edit bullet content and remove the meta stuff
        $(closestLi).find(".b_edit_meta").empty();

然后继续使用closestLi而不是引用清空this容器后不再存在的"this"

来自 .empty 上的 jQuery 文档:

如果要在不破坏元素数据或事件处理程序的情况下删除元素(以便以后可以重新添加它们),请改用 .detach()。