jQuery 点击函数找不到选择器
a = new object();
a.loadInterface();
$("button").click(function() {
a.doSomething();
});
问题是 a.loadInterface()
是将按钮加载到 DOM 中的,它发生在 $.post()
的末尾,因为来自服务器的数据需要填充按钮的某些属性。
所以我认为正在发生的事情是,JavaScript 读取 button.click
检测器,但当时没有按钮可供它绑定。
有什么想法吗?
a = new object();
a.loadInterface();
$("button").click(function() {
a.doSomething();
});
The problem is that a.loadInterface()
is what loads the button into the DOM, and it happens at the end of a $.post()
because data from the server is needed to populate some attributes of the button.
So what I think is going on is, JavaScript reads the button.click
detector but at that time there is no button for it to bind to.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对问题的评估是正确的。一个简单的解决方案是使用
delegate
。例如,Edit: more info on
delegate
and why it works on dynamically created elements after event binding can be found here: http://api.jquery.com/delegate/Your assessment of the problem is correct. A simple solution would be to use
delegate
. E.g.,Edit: more info on
delegate
and why it works on dynamically created elements after event binding can be found here: http://api.jquery.com/delegate/我认为你是对的。如果要根据帖子返回的数据创建按钮,则需要在帖子成功后执行的方法中绑定事件。
I think you are right. If you are creating button in based on the data returned from the post, you'll need to bind event in the method that is executed after the success of the post.