在jQuery中模拟Hover [英] Simulate Hover in jQuery
问题描述
目前,我有一些jQuery / Javascript代码,当用户将鼠标悬停在某些元素上时切换css类'ui-state-hovered',我想在konacha中编写测试来测试这段代码。
Currently, I have some jQuery/Javascript code that toggles the css class 'ui-state-hovered' when a user hovers the mouse over certain elements, and I want to write a test in konacha to test this piece of code.
如何在jQuery的帮助下在Javascript中编写此函数?
How would I write this function in Javascript with the help of jQuery?
如果是用户,则返回true悬停在元素$('。someClass li:first')上,类'ui-state-hovered'存在。
否则返回false。
Return true if when a user hovers over an element $('.someClass li:first'), the class 'ui-state-hovered' exists. Else return false.
我如何模拟将鼠标悬停在该元素上的用户?
How would I simulate a user hovering their mouse over that element?
推荐答案
尝试 mouseenter
和 mouseleave
$('.someClass li:first').mouseenter().mouseleave();
来自jQuery docs
From jQuery docs
调用
$(selector).hover(handlerIn,handlerOut)
是以下内容的简写:$(selector).mouseenter(handlerIn).mouseleave (handlerOut);
Calling
$(selector).hover(handlerIn, handlerOut)
is shorthand for:$(selector).mouseenter(handlerIn).mouseleave(handlerOut);
DEMO: http://jsfiddle.net/skram/wh5N9/
请尝试以下检查是否已在悬停中添加的类
Try below to check for an class that would have added in hover
$("#test").mouseenter(function() {
console.log('Has class hover ' + $(this).hasClass('ui-state-hovered'));
}).mouseleave(function() {
console.log('Has class hover ' + $(this).hasClass('ui-state-hovered'));
})
确保在之后注册以上内容.hover
Make sure the above is registered after .hover
DEMO: http://jsfiddle.net/skram/wh 5N9 / 2 /
这篇关于在jQuery中模拟Hover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!