[英]click and hover function on same element
I am having a small problem with mouse over/leave/click function. 我在将鼠标悬停/离开/单击功能时遇到了一个小问题。
It's working fine but after clicking the element it's losing the hover function. 它工作正常,但是单击该元素后,它将失去悬停功能。 I can't get the hover function once the element is clicked.
单击该元素后,我将无法获得悬停功能。
$('.bg').click(function () {
$(this).unbind("mouseenter mouseleave");
$('.bg').removeClass('active');
$(this).addClass('active');
}).hover(function () {
$(this).addClass('active');
}, function () {
$(this).removeClass('active');
});
jsfiddle -- jsfiddle-
http://jsfiddle.net/squidraj/SVkBs/1/ http://jsfiddle.net/squidraj/SVkBs/1/
Not sure where I am doing the mistake.Thanks. 不知道我在哪里做错了。谢谢。
您可以将:hover
CSS添加到这样的元素中: http : //jsfiddle.net/Agony/SVkBs/2/
What hover()
does is to bind a mouseenter
and a mouseleave
handler. hover()
作用是绑定mouseenter
和mouseleave
处理程序。 What unbind()
does is to remove those handlers from firing. unbind()
作用是从触发中删除那些处理程序。
So your code is doing exactly what you told it to; 因此,您的代码完全按照您的指示进行操作; when you click, it disables your hover handling.
单击时,它将禁用悬停处理。
$('.bg').click(function () {
/* remove that following line */
$(this).unbind("mouseenter mouseleave"); /* <-- this will UNBIND the 'hover' */
/* did you remove that previous line ? */
$('.bg').removeClass('active'); /* <-- this will remove 'active' from .bg */
$(this).addClass('active'); /* <-- this will add 'active' on this(.bg) */
}).hover(function () { /* <-- this will try to addClass 'active' on hover .. */
$(this).addClass('active');
}, function () { /* <-- this will try to removeClass on the otherHand (callback) */
$(this).removeClass('active');
});
ps. PS。 This function is working all right, you just didn't know what it is supposed to do (unbinding hover events).
此功能正常工作,您只是不知道该怎么做(解除绑定悬停事件)。
do it this way (toggling a class on hover) 这样操作(在悬停时切换课程)
$('.bg').hover(
function(){ $(this).addClass('active') },
function(){ $(this).removeClass('active') }
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.