jQuery:自动触发悬停

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11720903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:52:40  来源:igfitidea点击:

jQuery: Automatically trigger hover

jqueryhtmlcss

提问by Prem Paryani

I have a hover mousein mouseout setup as follows for a list item:

对于列表项,我有一个悬停 mousein mouseout 设置,如下所示:

$("#main-nav li a").hover(function() {
                $el = $(this);
                leftPos = $el.position().left;
                newWidth = $el.parent().width();
                $magicNav.stop().animate({
                    left: leftPos,
                    width: newWidth
                });
            }, function() {
                t1 = $(".current-menu-item a").position().left;
                t2 = $(".current-menu-item a").parent().width();
                $magicNav.stop().animate({
                    left: t1,
                    width: t2
                });    
            });

And i want to automatically trigger a hover on '.current-menu-item a' as soon as someone enters the website or the page is loaded.

并且我想在有人进入网站或页面加载时自动触发“.current-menu-item a”上的悬停。

At the moment, i use $(".current-menu-item a").trigger('hover');and it doesn't work.

目前,我使用$(".current-menu-item a").trigger('hover');它并不起作用。

Help?

帮助?

回答by Uttara

use this

用这个

$(document).ready(function(){
    $(".current-menu-item a").mouseover();
});

or

或者

$(window).load(function(){
    $(".current-menu-item a").mouseover();
});

回答by Utkanos

$(".current-menu-item a").trigger('mouseenter');

Hover is not a real event (it's a contrived one by jQuery, made up of mouseenterand mouseleave). In any case, it is a two-stage process, so not logically triggerable.

悬停不是真正的事件(它是由 jQuery 人为设计的,由mouseenter和 组成mouseleave)。无论如何,这是一个两阶段的过程,因此在逻辑上是不可触发的。

回答by CKKiller

you need to use mouseover not hover like so

你需要使用鼠标悬停而不是像这样悬停

$(".current-menu-item a").trigger('mouseover');

$(".current-menu-item a").trigger('mouseover');

回答by sumit

$('selector').trigger('eventName');