Jquery:为什么如果我有一个Jquery触发器在其他,第二个当发生多次
Jquery: Why if I had a jquery trigger inside in other, the second when occurs fire multiple times?
我有下面的代码,使toggle垂直下拉。当我在'ul> li> a'中多次执行mouseenter时,问题就会发生,然后在选择子菜单并进入其中后,第二个触发器会多次触发。
$('nav > ul > li > a').on('mouseenter',function(e){
var currentID = this;
var index = $('ul.topnav > li > a').index(this);
$(this).addClass('selected');
$('nav ul ul').css('margin-top',(38*parseInt(index)));
$(this).parent().find('ul').on('mouseenter',function(e){
//fire several times
console.log('hover');
$(currentID).addClass('selected');
}).on('mouseleave',function(e){
$(currentID).removeClass('selected');
//fire several times
console.log('end hover');
});
}).on('mouseleave',function(e){
$(this).removeClass('selected');
);
每次执行外部mouseenter
回调时,它都会将新的,额外的事件处理程序附加到ul
(它将与之前附加的处理程序做相同的事情)。
当鼠标进入ul
时,所有附加的事件处理程序都被调用。
您应该只附加一次事件处理程序,而不是每个mouseenter
。
每次将nav > ul > li > a
悬停时,都将事件绑定到this的子元素。
试试这个:
$('nav > ul > li > a').each(function(e){
$(this).on('mouseenter',function(e){
var index = $('ul.topnav > li > a').index(this);
$(this).addClass('selected');
$('nav ul ul').css('margin-top',(38*parseInt(index)));
}).on('mouseleave',function(e){
$(this).removeClass('selected');
);
var currentID = this;
$(this).parent().find('ul').on('mouseenter',function(e){
//fire several times
console.log('hover');
$(currentID).addClass('selected');
}).on('mouseleave',function(e){
$(currentID).removeClass('selected');
//fire several times
console.log('end hover');
});
});
相关文章:
- 为什么不'在JQuery中找到第二个css选择器的工作
- 第二个引导程序导航栏链接打开第一个(错误的)导航栏;jquery
- 如何针对JQuery中的第二个元素
- 第一个jquery事件正在发生,但没有发生第二个事件
- jQuery知道第一个转换已经完成,然后进行第二个转换
- jQuery Datepicker BeforeShowDay 第二个参数
- 访问jQuery中类的第二个成员
- JQuery Selectors:如何选择网页上第二个表单的提交按钮
- Jquery:选择tr元素的第二个td
- Jquery插件使用第二个实例's选项
- jQuery函数:为什么第一个事件的完成延迟到第二个事件完成
- JQuery:如果存在具有相同 ID 的元素,则选择第二个元素
- 第二个 Jquery json 在第一个 Jquery 完成之前被解雇
- 两个jquery插件,第二个不工作
- 为什么e.fn.e.init或x.fn.x.init(chrome调试器中的jQuery实例名称)中有第二个e或x
- 使用 jQuery 从字符串中获取倒数第二个字符的位置
- 使用 javascript / jquery 检查 URL 在第二个“/”之后是否有任何内容
- Android + jQuery - 第二个向左滑动不会执行
- JQuery 从第二个零开始播放 html5 视频,但如果用户暂停视频,则保留当前帧
- jQuery选择当前TR的第二个和第三个单元格