简体   繁体   中英

On click add class via core JS

How should I transfer this code in CORE JS file so no jQuery at all :

$('#menu-icon-trigger').click(function () {
    $('menu-icon').toggleClass('is-clicked');
});

One way to replicate jquery toggle class into JS is as follows:

function controlClass() {

    document.querySelector('menu-icon').classList.toggle('is-clicked');

}

document.querySelector('#menu-icon-trigger').addEventListener('click', controlClass);

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