jquery-ui - mouseleave 在 jquery 中不起作用

标签 jquery-ui jquery jquery-plugins jquery-selectors

我的 mouseleave 在我的 jquery 代码中不起作用

http://jsfiddle.net/alano/9Dr7T/29/

在下面提供我的js代码

mouseleave: function () {
    $(this).find("div:last").remove();
}

最佳答案

问题不在于 mouseleave 监听器,问题在于您如何绑定(bind)这些事件处理程序并取消绑定(bind)它们事情。 div 被删除,但每次 mouseenter 事件都会读取它。由于某种原因,在使用 .on()selector 过滤器时,mouseenter 事件并未解除绑定(bind)。可能和方式有关bubbling occurs when using the selector filter .

When a selector is provided, the event handler is referred to as delegated. The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector. jQuery bubbles the event from the event target up to the element where the handler is attached (i.e., innermost to outermost element) and runs the handler for any elements along that path matching the selector.

现在,我还不能 100% 确定为什么,但无论哪种方式,如果您使用像这样的直接绑定(bind)处理程序,它都会起作用:

$('.specialHover').on({
    mouseenter: function() {
        $("<div class='cta'>add image</div>").click(function() {
            var $me = $(this);
            $me.parent().unbind('mouseenter').children('img').attr(
                'src', 
                'http://www.onlinegrocerystore.co.uk/images/goodfood.jpg'
            );
            $me.remove();
        }).appendTo(this);
    },
    mouseleave: function() {
        $(this).find('div:last').remove();
    }
});

参见:http://jsfiddle.net/9Dr7T/35/

关于jquery-ui - mouseleave 在 jquery 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13338718/

相关文章:

JqueryUI DatePicker parseDate 比较

javascript - jQuery 用户界面 : Click event is being triggered on resize

jquery - 防止jsTree节点选择

jquery - 如何禁用引导日期范围选择器中的特定日期范围?

javascript - jQuery 多选可拖动/可排序

javascript - 内容更改而不刷新时无法显示正在加载的 GIF 图片

jquery - minDate 和 maxDate 选项在日期选择器中不起作用

php - 在 jQuery uploadify 中,在用户的文件路径上传之前将图像显示为缩略图

jQuery 自动将事件处理程序注册到通过 Ajax 响应添加到 DOM 的元素上的用户操作

javascript - jquery 对话框在 Accordion MVC razor 中不起作用