【问题标题】:Can jQuery click to call hover functions?jQuery可以点击调用悬停功能吗?
【发布时间】:2013-04-30 06:23:36
【问题描述】:

HTML:

<div class="hover">hover</div>

<div class="click">click</div>

jQuery :

$('.hover').hover(function(){
    alert("hovered!");
});

$('.click').click(function(){
    $('.hover').hover();
});

此代码无效,但我可以像$('.hover').hover() 一样吗?

游乐场: http://jsfiddle.net/wbFLH/


PS:我知道我可以做到

$('.hover').hover(function(){
    func();
});

$('.click').click(function(){
    func();
});

function func() {
    alert("something")
}

但我想知道,我可以通过使用“点击”功能悬停和调用悬停功能吗?

【问题讨论】:

    标签: javascript jquery click hover


    【解决方案1】:

    .hover() 使用.mouseenter().mouseleave(),因此您必须改为触发.mouseenter/.mouseleave

    来自jQuery .hover docs

    .hover() 方法绑定了 mouseenter 和 mouseleave 事件的处理程序。您可以使用它在鼠标位于元素内时简单地将行为应用于元素。

    调用 $(selector).hover(handlerIn, handlerOut) 是以下的简写:

    $(selector).mouseenter(handlerIn).mouseleave(handlerOut);

    $('.hover').hover(function(){
        $('code').append('l');
    });
    
    $('.click').click(function(){
        $('.hover').mouseenter();
    });
    

    EXAMPLE

    【讨论】:

    • 该死,7 秒,我不得不删除我的(赞成的)答案,因为它没用 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多