添加和删除点击/悬停事件处理程序
Adding and removing click/hover event handlers jQuery
遇到点击和悬停事件混合的问题。
单击一个不活动的元素会切换一个活动的类并绑定一个悬停事件。悬停在当前活动的元素上,将显示先前隐藏的块(span.rate)点击悬停元素应该隐藏它,删除悬停事件,并切换父元素上的活动类,使其不再是"活动"。
单击悬停事件不会删除事件并切换为活动状态。这里有一些关于互斥选项的其他逻辑,尽管这些都工作得很好。
http://jsfiddle.net/65yY3/15/当前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
。 Demo1 -
或者在子进程中添加
e.stopPropagation();
来阻止从父进程到子进程的事件冒泡。以及接下来$('.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 });
输入链接描述
$('.example li a') instead of $('.example li a)')
相关文章:
- keyup事件处理程序更改焦点不适用于快速键入
- 提示使用服务器端事件处理程序激活JavaScript
- 鼠标悬停事件影响列表中所有行中的按钮,而不仅仅是特定按钮
- 将事件处理程序绑定到任何可能的事件
- 正在将事件处理程序添加到不存在的类
- dropdown.js中的复杂事件处理
- 在循环中附加事件处理程序时出现浏览器性能问题
- 在同一个javascript事件处理程序中调用不同的函数
- 无法让悬停事件使用 jQuery UI 持续时间参数或 CSS 处理目标元素的同级元素
- 单击事件执行单击和悬停事件处理程序,即使单击事件处理程序取消绑定所有处理程序也是如此
- 在 HTML 中传递多个参数鼠标悬停事件处理程序
- 为什么悬停在委托的事件处理程序中不起作用
- 如何在鼠标悬停事件处理程序中创建单击事件处理程序
- 处理谷歌图表表格的悬停事件
- JQuery悬停事件处理程序运行次数过多
- .each()绑定悬停事件并传递一个变量给处理函数
- 延迟处理悬停事件
- 鼠标悬停事件处理程序不工作
- 添加和删除点击/悬停事件处理程序
- 如何在 jQuery 悬停事件处理程序中使用 $(this)