无法确定为什么.hover正在工作但是鼠标输入不是?

时间:2014-12-10 09:21:53

标签: javascript jquery hover

我想要它,以便您可以将鼠标悬停在动态添加的内容上。我发现代码允许滚动菜单下拉菜单,但仅适用于.hover(),但我想动态添加这些菜单下拉菜单。我找到了关于click mouseenter和mouseleave的信息,但这似乎不像我做的那样工作。两者都是发布代码。

我本质上希望拥有第一个代码的功能,但是这样做是为了动态添加元素,因为.hover是从页面创建绑定的,我不能轻易做到这一点,还没有找到一个可行的解决方案。我不知道为什么第二个解决方案不能像悬停一样工作。如果有任何可以,他们可以向我解释那将是伟大的,因为我读到的一切都说它应该工作。任何帮助将不胜感激。提前谢谢。

每个代码段的第一行是这两个代码之间的唯一区别。但是,我不知道是否可以成为我的问题,因为这个问题导致了我的问题所以我加了两个

工作代码

 $(".dropdown > li").hover(function() {

         var $container = $(this),
             $list = $container.find("ul"),
             $anchor = $container.find("a"),
             height = $list.height() * 1.1,       // make sure there is enough room at the bottom
             multiplier = height / maxHeight;     // needs to move faster if list is taller
        //alert("Hi");
        // need to save height here so it can revert on mouseout            
        $container.data("origHeight", $container.height());

        // so it can retain it's rollover color all the while the dropdown is open
        $anchor.addClass("hover");

        // make sure dropdown appears directly below parent list item    
        $list
            .show()
            .css({
                paddingTop: $container.data("origHeight")
            });

        // don't do any animation if list shorter than max
        if (multiplier > 1) {
            $container
                .css({
                    height: maxHeight,
                    overflow: "hidden"
                })
                .mousemove(function(e) {
                    var offset = $container.offset();
                    var relativeY = ((e.pageY - offset.top) * multiplier) - ($container.data("origHeight") * multiplier);
                    if (relativeY > $container.data("origHeight")) {
                        $list.css("top", -relativeY + $container.data("origHeight"));
                    };
                });
        }

    }, function() {

        var $el = $(this);

        // put things back to normal
        $el
            .height($(this).data("origHeight"))
            .find("ul")
            .css({ top: 0 })
            .hide()
            .end()
            .find("a")
            .removeClass("hover");

    });  

不工作代码

  $(document).on("mouseenter", ".dropdown > li", function(){

         var $container = $(this),
             $list = $container.find("ul"),
             $anchor = $container.find("a"),
             height = $list.height() * 1.1,       // make sure there is enough room at the bottom
             multiplier = height / maxHeight;     // needs to move faster if list is taller
        alert("Got here");
        // need to save height here so it can revert on mouseout            
        $container.data("origHeight", $container.height());

        // so it can retain it's rollover color all the while the dropdown is open
        $anchor.addClass("hover");

        // make sure dropdown appears directly below parent list item    
        $list
            .show()
            .css({
                paddingTop: $container.data("origHeight")
            });

        // don't do any animation if list shorter than max
        if (multiplier > 1) {
            $container
                .css({
                    height: maxHeight,
                    overflow: "hidden"
                })
                .mousemove(function(e) {
                    var offset = $container.offset();
                    var relativeY = ((e.pageY - offset.top) * multiplier) - ($container.data("origHeight") * multiplier);
                    if (relativeY > $container.data("origHeight")) {
                        $list.css("top", -relativeY + $container.data("origHeight"));
                    };
                });
        }

    }, function() {

        var $el = $(this);

        // put things back to normal
        $el
            .height($(this).data("origHeight"))
            .find("ul")
            .css({ top: 0 })
            .hide()
            .end()
            .find("a")
            .removeClass("hover");

    });  

1 个答案:

答案 0 :(得分:1)

如果您想为hover()使用委派事件处理程序,则需要使用mouseentermouseleave

$(document).on("mouseenter", ".dropdown > li", function () {

    var $container = $(this),
        $list = $container.find("ul"),
        $anchor = $container.find("a"),
        height = $list.height() * 1.1, // make sure there is enough room at the bottom
        multiplier = height / maxHeight; // needs to move faster if list is taller
    alert("Got here");
    // need to save height here so it can revert on mouseout            
    $container.data("origHeight", $container.height());

    // so it can retain it's rollover color all the while the dropdown is open
    $anchor.addClass("hover");

    // make sure dropdown appears directly below parent list item    
    $list.show()
        .css({
        paddingTop: $container.data("origHeight")
    });

    // don't do any animation if list shorter than max
    if (multiplier > 1) {
        $container.css({
            height: maxHeight,
            overflow: "hidden"
        })
            .mousemove(function (e) {
            var offset = $container.offset();
            var relativeY = ((e.pageY - offset.top) * multiplier) - ($container.data("origHeight") * multiplier);
            if (relativeY > $container.data("origHeight")) {
                $list.css("top", -relativeY + $container.data("origHeight"));
            };
        });
    }

});
$(document).on("mouseleave", ".dropdown > li", function () {
    var $el = $(this);

    // put things back to normal
    $el.height($(this).data("origHeight"))
        .find("ul")
        .css({
        top: 0
    })
        .hide()
        .end()
        .find("a")
        .removeClass("hover");

});