简体   繁体   中英

Jquery to pure javascript - simple function (click and toggle)

As I do not know js I'm looking for help - I would need this simple jquery code written in pure javascript:

$( document ).ready(function() {
    $(".mobile-menu-cta").click(function() {
          $(".mobile-menu").toggle();
    });
});

This mimics the toggle behaviour:

document.addEventListener('DOMContentLoaded', function(e) {
  const menuCta = document.querySelector('.mobile-menu-cta');
  const menu = document.querySelector('.mobile-menu');

  menuCta.addEventListener('click', function(e) {
    e.preventDefault();

    if (menu.style.display === 'none') {
      menu.style.display = 'block';
    } else {
      menu.style.display = 'none';
    }
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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