jQuery可以点击调用悬停功能吗?

l2a*_*lba 3 javascript jquery click hover

HTML:

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

<div class="click">click</div>
Run Code Online (Sandbox Code Playgroud)

jQuery:

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

$('.click').click(function(){
    $('.hover').hover();
});
Run Code Online (Sandbox Code Playgroud)

这段代码不起作用,但我可以这样做$('.hover').hover()吗?

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


PS:我知道我可以这样做

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

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

function func() {
    alert("something")
}
Run Code Online (Sandbox Code Playgroud)

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

ᾠῗᵲ*_*ᵲᄐᶌ 8

.hover()使用.mouseenter(),.mouseleave()所以你必须触发.mouseenter/.mouseleave.

来自jQuery .hover docs

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

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

$(选择器).mouseenter(handlerIn).mouseleave(handlerOut);

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

$('.click').click(function(){
    $('.hover').mouseenter();
});
Run Code Online (Sandbox Code Playgroud)