简体   繁体   中英

Need to bind onclick event on drawer button

I am working with material design lite in my project and I need to attach a function to the click event of the drawer button. I thought it was trivial, so I added this code:

$(document).ready(function(){ 
    $('.mdl-layout__drawer-button).on('click', function(){
       console.log('click');
});

but then I discovered that the drawer button is added by mdl.js as the very last element after document.ready, so when I run my code, the button is not yet ready.

Any idea on how to get the selector when it is ready?

You need delegation : https://learn.jquery.com/events/event-delegation/

$(document.body).on('click', '.mdl-layout__drawer-button', function(){
  console.log('click');
});

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