简体   繁体   English

简单的jquery点击事件脚本,它是一个循环

[英]Simple jquery click event script, its a loop

I have a simple jQuery script. 我有一个简单的jQuery脚本。 This is the script: 这是脚本:

var menu = $('#nav .menu');

$('li', menu).mouseenter(function() {
    $(this).find('.sub-menu').slideDown();
}).mouseleave(function(){
    $(this).find('.sub-menu').slideUp();
});

This script open a submenu. 此脚本打开一个子菜单。 But i have a problem with this script. 但是这个脚本有问题。 If you go over it quickly. 如果你快点过去的话。 The script launch every time. 该脚本每次都会启动。 When you go over the item verry quickly. 当你快速浏览项目时。 The menu open a lot of times. 菜单打开很多次。 How can i fix this? 我怎样才能解决这个问题?

Thank for help 谢谢你的帮助

use jQuery's .stop() function. 使用jQuery的.stop()函数。 Passing in the necessary arguments ex. 传递必要的论据ex。 .stop(true,true) , .stop(true) .stop(true,true) .stop(true)

$('li', menu).mouseenter(function() {
    $(this).find('.sub-menu').stop().slideDown();
}).mouseleave(function(){
    $(this).find('.sub-menu').stop().slideUp();
});

or passing this as the context seems a little neater to me - it does the same thing as .find() 或者传递这个因为上下文似乎对我来说有点整洁 - 它与.find()的作用相同

$('li', menu).mouseenter(function() {
    $('.sub-menu',this).stop().slideDown();
}).mouseleave(function(){
    $('.sub-menu',this).stop().slideUp();
});

Use this way: 使用这种方式:

$('#nav .menu li').hover(function() {
   $('.submenu').stop().slideDown();
}, function(){
   $('.submenu').stop().slideUp();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM