admin管理员组

文章数量:1333186

HTML :

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

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

jQuery :

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

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

This code isn't work , But can I do like $('.hover').hover() ?

Playground : /


PS : I know I can do like

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

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

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

But I want to know , Can I hover and call hover function via using "click" function ?

HTML :

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

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

jQuery :

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

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

This code isn't work , But can I do like $('.hover').hover() ?

Playground : http://jsfiddle/wbFLH/


PS : I know I can do like

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

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

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

But I want to know , Can I hover and call hover function via using "click" function ?

Share asked May 6, 2013 at 13:23 l2aelbal2aelba 22.2k23 gold badges108 silver badges144 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

.hover() uses .mouseenter() and .mouseleave() so you have to trigger .mouseenter/.mouseleave instead.

From 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

本文标签: javascriptCan jQuery click to call hover functionsStack Overflow