jQuery Jquery如何在动态创建的元素上绑定点击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17558167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:17:57 来源:igfitidea点击:
Jquery how to bind click event on dynamically created elements?
提问by MonsterMMORPG
I tried the below code but it is not working
我尝试了下面的代码,但它不起作用
<script type="text/javascript">
$(document).ready(function () {
$('body').on('click', '.pg_previous,.pg_next', function () {
jQuery("img.lazy").lazy({});
alert('ddsda');
});
});
</script>
Jquery 1.9.1
jQuery 1.9.1
回答by RicardoE
here try this:
在这里试试这个:
<script type="text/javascript">
$(function(){
$('body').on('click', '.pg_previous,.pg_next', function () {
jQuery("img.lazy").lazy({});
alert('ddsda');
});
});
</script>
回答by Sushanth --
You forgot the DOM ready
handler
你忘了DOM ready
处理程序
Encase your code inside the ready handler and should work fine unless you have any errors showing up in our console.
将您的代码封装在就绪处理程序中,除非您在我们的控制台中显示任何错误,否则应该可以正常工作。
$(function() {
// Your code here
});