简体   繁体   English

动态创建的按钮上的点击事件

[英]Click events on dynamically created buttons

I am building a web app, and the UI will have some dynamically created buttons. 我正在构建一个Web应用程序,并且UI将具有一些动态创建的按钮。 I struggled with click events on those buttons, but made it work by having click events on static tags that dynamically filled. 我为那些按钮上的单击事件而苦苦挣扎,但是通过使动态填充的静态标签上的单击事件使它能够正常工作。

While this works, it doesn't seem like a great way to do this. 尽管此方法有效,但这似乎不是一种好方法。

Is there a more semantic way of doing this? 有没有更多的语义方式可以做到这一点? Or better still, a way to directly click the dynamically created button, and have a function triggered? 或者更好的方法是直接单击动态创建的按钮并触发功能?

Use .on() method: 使用.on()方法:

$(document).on('click', '.button-class', function() {
    // Clicked!
});

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

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