[英]How to fire hover for anchor tag through jquery?
I have a link. 我有一个链接。 It hover has some background effects.
它悬停有一些背景效果。
I want to fire this hover through jquery. 我想通过jQuery触发此悬停。
You can fire the javascript hover ( mouseover
of mouseenter
or mousemove
or something, depending on the js lib), but you can't fire the CSS :hover state with Javascript. 您可以触发javascript悬停(取决于js lib的
mouseenter
或mousemove
mouseover
),但是无法使用Javascript触发CSS:hover状态。
You can only use jQuery to fire the event IF jQuery was used to attach the event. 如果使用jQuery附加事件,则只能使用jQuery触发事件。
Use JQuery hover() wrapped in document.ready
, so that it automatically fires up when a hover happens over the link 使用包装在
document.ready
JQuery hover() ,以便在链接上发生悬停时自动触发
$(document).ready(function() {
$("#YourLinkId").hover(functionToHandleWhenMouseEnters,
functionToHandleWhenMouseLeaves);
});
function functionToHandleWhenMouseEnters() {
$(this).css({background : red});
}
function functionToHandleWhenMouseLeaves() {
$(this).css({background: white});
}
Edit: 编辑:
I am note very sure if this is what you wanted, but as per David's comment, it might not be it. 我非常确定这是否是您想要的,但是根据David的评论,可能不是。 If you want to programatically fire (trigger) a hover, then you should use jQuery trigger()
如果要以编程方式触发(触发)悬停,则应使用jQuery trigger()
$("#YourLinkId").trigger("mouseover");
I'm not sure, but I don't believe you can fire the :hover
state for a CSS element. 我不确定,但是我不相信您可以为CSS元素触发
:hover
状态。 I would suggest you handle the hover events for the element and add/remove a special class for your element, for example: 我建议您处理元素的悬停事件,并为元素添加/删除一个特殊的类,例如:
CSS: CSS:
#myElement.myClass { }
jQuery: jQuery的:
$("#myElement").hover(
function(){
//mouse in
$(this).addClass("myClass");
}, function(){
$(this).removeClass("myClass");
});
$('#hrefID').trigger('mouseover'); //triggers hover
//your 'hover' code runs....
$('#hrefID').trigger('mouseout'); //removes hover
//your 'mouse out' portion of hover code runs here...
see this link on jQuery Trigger() 请参阅jQuery Trigger()上的此链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.