javascript - jQuery 可以点击调用悬停函数吗?

标签 javascript jquery click hover

HTML :

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

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

jQuery :

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

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

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

Playground : http://jsfiddle.net/wbFLH/


PS : 我知道我可以这样做

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

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

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

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

最佳答案

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

来自 jQuery .hover docs

The .hover() method binds handlers for both mouseenter and mouseleave events. You can use it to simply apply behavior to an element during the time the mouse is within the element.

Calling $(selector).hover(handlerIn, handlerOut) is shorthand for:

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

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

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

EXAMPLE

关于javascript - jQuery 可以点击调用悬停函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16399727/

相关文章:

php - 根据尺寸更改价格并显示尺寸

javascript - 在滚动部分的末尾添加动画

javascript - 有没有办法在不添加事件的情况下对点击使用react?

javascript - 在元素外部单击时切换元素并移除其可见性 - JavaScript

javascript - 当函数名称是保留字时如何使用 Redux Saga "call"函数以及如何进行链式函数调用?

javascript - 隐藏 URL ui-sref (ui.router) 中的参数

javascript - 在 Javascript 中填充 Meta 标记

php - 使用 AJAX/Jquery 实时查询用户名

javascript - 如何将字符串传递给 Bootstrap 模式?

javascript - JQuery取消文档中所有点击事件的方法