简体   繁体   English

需要在抽屉按钮上绑定onclick事件

[英]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. 我正在项目中使用Material Design Lite,并且需要将功能附加到抽屉按钮的click事件。 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. 但是后来我发现mdl.js将抽屉按钮添加为document.ready之后的最后一个元素,因此当我运行代码时,按钮尚未准备好。

Any idea on how to get the selector when it is ready? 关于准备就绪时如何获取选择器的任何想法?

You need delegation : https://learn.jquery.com/events/event-delegation/ 您需要delegationhttps : //learn.jquery.com/events/event-delegation/

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

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

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