jquery trigger hover on anchor

半世苍凉 提交于 2019-11-28 11:55:13

You are on the right track, the problem is the extra # in the selector, just remove the first hash:

$("a#trigger").trigger('mouseenter');

Note that since IDs must be unique, there is no need to specify the element type, $('#trigger') is more efficient.

Also note that:

Deprecated in jQuery 1.8, removed in 1.9: The name "hover" used as a shorthand for the string "mouseenter mouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions.

Your jQuery selector should be written as e.g.

$('a#trigger');

instead of $('#a#trigger');

In jQuery a # in a selector matches an id. In this case trigger is an id, but a is an HTML element and requires no prefix.

Your final code would be:

$("a#trigger").trigger('mouseenter');
$("a#trigger").trigger('hover');
$("a#trigger").trigger('mouseover');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!