[英]Adding and removing click/hover event handlers jQuery
遇到混合单击和悬停事件的问题。
单击不活动的li元素可切换活动的类并绑定悬停事件。 将鼠标悬停在现在的活动元素上会显示一个先前隐藏的块(span.rate)。单击该悬停的元素应该将其隐藏,删除悬停事件并在父级上切换活动类,使其不再“活动”。
单击悬停的事件不会删除事件并切换为活动状态。 关于互斥选项,还有一些其他逻辑,尽管一切正常。
jsfiddle关于如何将它们放在一起:
当前的js:
ps = {
psToggle: 0,
init: function () {
$(document).ready(function () {
$('.example li a)').on('click', function(e) {
e.preventDefault();
var that = $(this);
if (that.parent().hasClass('paired')) {
if (rm.psToggle === 0) {
that.toggleClass('active');
that.find('.rate').fadeToggle(50);
rm.psToggle = 1;
} else {
if (that.hasClass('active')) {
that.toggleClass('active');
that.find('.rate').fadeToggle(50);
rm.psToggle = 0;
} else {
$('.paired a').toggleClass('active');
that.find('.rate').fadeToggle(50);
//Call message function
}
}
rm.pControl();
} else {
that.toggleClass('active');
that.find('.rate').fadeToggle(50);
rm.pControl();
}
});
});
},
pControl: function () {
//Unbind events to all control items excluding 1st item.
$('.example li a').off('hover');
$('.example li a .rate').off('click');
$('.example .active').each(function(i) {
$(this).on('hover', function() {
$(this).find('.rate').fadeToggle(50);
});
});
$('.example li a.active .rate').on('click', function() {
//Remove hover/hide and toggle active state
$(this).off('hover');
$(this).hide();
$(this).parent().toggleClass('active');
rm.pControl(); //rebind new active classes
});
}
};
ps.init();
查看以下演示。
无论是点击事件被炒鱿鱼的, .rate
是孩子a
。
$('.example li a.active .rate').on('click'...
和
$('.example li a').on('click'...
.rate
的单击。 演示1 或添加e.stopPropagation();
给孩子停止事件从父母到孩子的冒泡。 演示2
$('.example li a.active .rate').on('click', function(e) { e.stopPropagation(); //Remove hover/hide and toggle active state $(this).off('hover'); $(this).hide(); $(this).parent().toggleClass('active'); ps.pControl(); //rebind new active classes });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.