Mouseout和mouseleave不起作用 [英] Mouseout and mouseleave not working

查看:309
本文介绍了Mouseout和mouseleave不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mouseoutmouseleave方法,但两者均无法正常工作.我试图解决它,但找不到解决方案.我的代码看起来不错,没有错误,所以我想知道为什么它不起作用.这是示例代码链接

Hi I am using mouseout and mouseleave methods but both are not working. I tried to fix it but cant find the solution. My code looks fine, it has no errors so I want to know why it is not working. Here is example code link

$(".chzn-select").chosen()
$(function(){
    $('a').click(function(){
        $('.mydiv').addClass('redbrd')
    })

    $('.redbrd').live('mouseover', function(){
        var htm= '<div id="mmt">some text</div>'
        $('body').append(htm)
    })
    $('.redbrd').live('mouseout', function(){
        $('#mmt').remove()
    })
})

推荐答案

在您的小提琴页面上,由于该部分以外的代码比较复杂,因此可能检测到鼠标事件,但是请使用此代码尽你所能:

Looking at your fiddle page, there might be some issues with the mouse events being detected due to the complication of the code aside from this part, however using this should get you most of the way there:

$(function() {
    $(".chzn-select").chosen();

    $('a').click(function() {
        $('.mydiv').removeClass().addClass('redbrd mydiv');// NOTE this is in case your other question comes into play with this one.
    });
    $('body').on('mouseenter', '.redbrd', function() {
       $('body').append('<div class="mmt">some text</div>');
    });
    $('body').on('mouseleave', '.redbrd', function() {
        $('.mmt').remove();
    });
});

查看后,您在选择的内容之后将li添加到页面中.

After review, your adding li to the page after your chosen thing.

这应该适用于此

$(".chzn-select").chosen();
$(function() {
    $('a').click(function() {
        $('.mydiv').addClass('redbrd');

        $('.redbrd').on('mouseover', 'li', function(e) {
            var $target = $(e.target);
            if ($('#mmt').length === 0) {
                var htm = '<div id="mmt">' + $target.text() + ' some text</div>';
                $('body').append(htm);
            }
        });
        $('.redbrd').on('mouseout', function() {
            $('#mmt').remove();
        });
    });
});

在此处更新了小提琴: http://jsfiddle.net/JtQHY/1/可以测试.

Updated your fiddle here:http://jsfiddle.net/JtQHY/1/ so you can test it.

这篇关于Mouseout和mouseleave不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆