简体   繁体   English

jQuery .hover slideDown slideUp正常,但是单击不正常

[英]JQuery .hover slideDown slideUp working but ,click not working

I am using bootstrap 3 and I have a dropdown menu. 我正在使用bootstrap 3,并且有一个下拉菜单。 Instead of using the default animation I have used a JQuery slideDown and slideUp . 我没有使用默认的动画,而是使用了JQuery slideDownslideUp Only issue I am having is when I hover it works fine but when I click the menu it displays properly and slidesDown works, second click then slideUP works, then on the third click to check if will work again, there is no response. 我唯一遇到的问题是,当我将鼠标悬停在其上时,它可以正常工作,但是当我单击菜单时,它可以正确显示并且slidesDown可以工作,第二次单击,然后可以使用slideUP ,然后在第三次单击时检查是否可以再次工作,则没有响应。 I have seen many examples and different methods but I am a novice and haven't had much luck. 我看过很多例子和不同的方法,但是我是新手,运气不太好。

Relevant Code that works: 有效的相关代码:

$(document).ready(function () {
   $('.navbar-default .navbar-nav > li.dropdown').hover(function () {
      $('ul.dropdown-menu', this).stop(true, true).slideDown('fast');
      $(this).addClass('open');
   }, function () {
      $('ul.dropdown-menu', this).stop(true, true).slideUp(100);
      $(this).removeClass('open');
   }); 
});

Relevant Code that does't work: 无效的相关代码:

$(document).ready(function () {
   $('.navbar-default .navbar-nav > li.dropdown').click(function () {
      $('ul.dropdown-menu', this).stop(true, true).slideDown('fast');
      $(this).addClass('open');
   }, function () {
      $('ul.dropdown-menu', this).stop(true, true).slideUp(100);
      $(this).removeClass('open');
   }); 
});

In the end I would like to add $('#target').on('click hover', function () { so if the user hovers over the menu and accidentally clicks, it closes the menu and they can reopen by either hovering over it again or clicking it open. 最后,我想添加$('#target').on('click hover', function () {因此,如果用户将鼠标悬停在菜单上并意外单击,它将关闭菜单,并且可以通过将其悬停来重新打开再次单击它或单击将其打开。

I'm not sure how to remove the hover function when clicking. 我不确定单击时如何删除悬停功能。

The click() method takes only one parameter as its argument. click()方法仅将一个参数作为其参数。 You can use slideToggle() and toggleClass() 您可以使用slideToggle()toggleClass()

$(document).ready(function () {
   $('.navbar-default .navbar-nav > li.dropdown').click(function () {
      $('ul.dropdown-menu', this).stop(true, true).slideToggle('fast');
      $(this).toggleClass('open');
   }); 
});

Hover takes two functions as parameters: ( http://api.jquery.com/hover/ ) 悬停使用两个函数作为参数:( http://api.jquery.com/hover/

jQuery.hover(onHoverIn, onHoverOut); 

Click only takes one. 单击只需要一个。 ( http://api.jquery.com/click/ ) http://api.jquery.com/click/

You would need to do something like: 您将需要执行以下操作:

$(document).ready(function () {
   $('.navbar-default .navbar-nav > li.dropdown').click(function () {
      if ($(this).hasClass('open')) {
         $('ul.dropdown-menu', this).stop(true, true).slideUp(100);
         $(this).removeClass('open');
      } else {
         $('ul.dropdown-menu', this).stop(true, true).slideDown('fast');
         $(this).addClass('open');
      }
   }); 
});

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

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