jquery preventDefault.



  • There's a menu that has two versions. I'd like to first-level references to be banned in the mobile menu. Doing the following:

      $(document).on('click', '.nav.open a.parent', function(event) {       
          event.preventDefault();   //Блокируем переход по родительскому пункту
          console.log('Блокируем ссылку');
          $(this).parent().siblings('li').not($(this).parent()).removeClass('open');
          $(this).parent().toggleClass('open');       
         // return false;
      });

    where a combination of classes ...nav.open only occurs when a menu is pressed on the button in the mobile version of the website. The code's working on the second time, i.e. if the first time we go to the site, insert a mobile phone emulation (worm turns around), open the menu and move on the first-level reference, there's a shift with the page overload. If the operation is repeated, it will work event.preventDefault.

    I've reached a conclusion to the console of the element $('nav.open a.parent'), he's just showing up when he presses on the button with the menu show. http://joxi.ru/DmBXGOZhNLP0oA ♪

    What's the problem? Why event.preventDefault(); not working first?



  •   $(document).on('click', '.nav a.parent', function(event) { 
         var parent = $(this).parent(); 
         if (parent.hasClass('open')) {
             event.preventDefault();   //Блокируем переход по родительскому пункту
              console.log('Блокируем ссылку');
         }   
    
     parent.siblings('li').not(parent).removeClass('open');
     parent.toggleClass('open');       
    // return false;
    

    });

    Try this, it's supposed to work, but it's better and logical for the processor to hang out, and when it's necessary.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2