jquery将事件绑定到动态加载的html元素
使用 jquery,我们可以将事件处理程序附加到页面中存在的元素,这是在 document.ready() 函数内完成的。现在我的困难是我在下载文档后稍后加载一些元素,例如链接等(使用ajax请求)。因此,这些新元素无法与我在 page.xml 中定义的处理程序绑定。有什么方法可以知道后续的 ajax 查询何时完成,然后在其中我可以绑定我的事件处理程序。
提前致谢
using jquery we can attach event handlers to the elements present in page, this is done inside document.ready() function. Now my difficulty is i have some elements like links etc loaded later (using ajax request) after document is downloaded . So those new elements are failing to bind with the handlers I defined in page. Is there any way to know when followed ajax queries are finish and then inside that i can bind my event handlers.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
各种
ajax
方法接受回调,您可以将处理程序绑定到新元素。您还可以将事件委托与
delegate()
< support>[docs] 方法或live()
[docs] 方法。事件委托的概念是,您不将处理程序绑定到元素本身,而是绑定到页面加载时存在的某个父容器。
事件从容器内的元素中冒出,当事件到达容器时,将运行选择器以查看接收事件的元素是否应调用处理程序。
例如:
实例: http://jsfiddle.net/5jKzB/
所以您将一个处理程序绑定到
some_container
,并将一个选择器传递给.delegate()
,在本例中查找"a.link"
。当在
some_container
内单击与该选择器匹配的元素时,将调用处理程序。因此您可以看到,
"a.link"
元素何时添加到 DOM 中并不重要,只要some_container
在页面加载时存在即可。live()
[docs] 方法是相同的,只不过容器是document
,因此它处理页面上的所有点击。The various
ajax
methods accept a callback where you can bind the handlers to the new elements.You can also use event delegation with the
delegate()
[docs] method or thelive()
[docs] method.The concept of event delegation is that you do not bind the handler to the element itself, but rather to some parent container that exists when the page loads.
The events bubble up from the elements inside the container, and when it reaches the container, a selector is run to see if the element that received the event should invoke the handler.
For example:
Live Example: http://jsfiddle.net/5jKzB/
So you bind a handler to
some_container
, and pass a selector to.delegate()
that looks for"a.link"
in this case.When an element that matches that selector is clicked inside of
some_container
, the handler is invoked.So you can see that it doesn't matter when the
"a.link"
elements are added to the DOM, as long as thesome_container
existed when the page loaded.The
live()
[docs] method is the same, except that the container is thedocument
, so it processes all clicks on the page.然后您将需要使用
.live()
。看看 http://api.jquery.com/live/。示例:
在上面的示例中,任何 A 元素,无论是已经存在的还是在文档加载后加载的,都会触发 click 事件。
Then you'll want to use
.live()
. Have a look at http://api.jquery.com/live/.Example:
In the example above, any A elements, whether already existing or loaded after the document is loaded, will trigger the click event.
这些答案现在已过时,因为自 jQuery 1.7 版本以来 .live() 方法已被弃用。对于 jQuery 1.7+,您可以使用 .on() 将事件处理程序附加到父元素
These answers are now obsolete since the .live() method has been deprecated since version 1.7 of jQuery. For jQuery 1.7+ you can attach an event handler to a parent element using .on()
使用 .live() 绑定它们。它将事件处理程序附加到与选择器匹配的任何元素,即使页面上尚不存在该元素。
bind them using .live(). It will attach the event handler to any element that matches the selector even if it doesn't exist on the page yet.