jQuery绑定实时事件 [英] jQuery binding live events
问题描述
将实时事件绑定到动态创建的内容时遇到麻烦.
I am having troubles binding live events to dynamically created content.
我有一个页面,该页面通过ajax调用加载html页面.加载的html页面包含一些javascript,然后将它们执行.执行的javascript创建一些元素.
I have a page which loads a html page through an ajax call. The loaded html page contains some javascript which is then executed. The executed javascript creates some elements.
我想将click事件处理程序绑定到那些元素,但是在将它们绑定(使用.live,.delegate和普通的老式.click)之后,单击它们之后什么也没有发生.
I want to bind a click event handler to those elements, however after binding them (using .live, .delegate and plain old .click) nothing happens after clicking on them.
我能够通过调用.html来检索元素的内容,但是由于某种原因我无法在其上绑定事件...
I am able to retrieve the contents of the elements (by calling .html) but I can't bind events on them for some reason...
我也尝试添加onclick ="alert('test');"元素,然后通过javascript将它们吐出来,但无济于事...
I have also tried adding onclick="alert('test');" to the elements before spitting them out through javascript but to no avail...
有人知道解决方案吗?
谢谢
推荐答案
John,
看看.livequery()插件.这将为您提供包括.live()事件以及其他回调方案的完整范围.
take a look at the .livequery() plugin. this will give you a full range that includes the .live() event plus other callback scenarios.
http://docs.jquery.com/Plugins/livequery
吉姆
这篇关于jQuery绑定实时事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!