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
代替。
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();
});
关于javascript - jQuery 可以点击调用悬停函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16399727/