jquery将事件绑定到动态加载的html元素

发布于 2024-11-16 00:46:09 字数 198 浏览 1 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

·深蓝 2024-11-23 00:46:09

各种 ajax 方法接受回调,您可以将处理程序绑定到新元素。

您还可以将事件委托与 delegate()< support>[docs] 方法或 live()[docs] 方法。

事件委托的概念是,您不将处理程序绑定到元素本身,而是绑定到页面加载时存在的某个父容器。

事件从容器内的元素中冒出,当事件到达容器时,将运行选择器以查看接收事件的元素是否应调用处理程序。

例如:

<div id="some_container"> <!-- this is present when the page loads -->

    <a class="link">some button</a>  <!-- this is present when the page loads -->
    <a class="link">some button</a>  <!-- this is present when the page loads -->
    <a class="link">some button</a>  <!-- this is present when the page loads -->


    <a class="link">some button</a>  <!-- this one is dynamic -->
    <a class="link">some button</a>  <!-- this one is dynamic -->
    <a class="link">some button</a>  <!-- this one is dynamic -->

    <span>some text</span>  <!-- this one won't match the selector -->
    <span>some text</span>  <!-- this one won't match the selector -->

</div>

实例: http://jsfiddle.net/5jKzB/

所以您将一个处理程序绑定到 some_container,并将一个选择器传递给 .delegate(),在本例中查找 "a.link"

当在 some_container 内单击与该选择器匹配的元素时,将调用处理程序。

$('#some_container').delegate('a.link', 'click', function() {
    // runs your code when an "a.link" inside of "some_container" is clicked
});

因此您可以看到,"a.link" 元素何时添加到 DOM 中并不重要,只要 some_container 在页面加载时存在即可。

live()[docs] 方法是相同的,只不过容器是 document,因此它处理页面上的所有点击。

$('a.link').live('click',function() {
    // runs your code when any "a.link" is clicked
});

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 the live()[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:

<div id="some_container"> <!-- this is present when the page loads -->

    <a class="link">some button</a>  <!-- this is present when the page loads -->
    <a class="link">some button</a>  <!-- this is present when the page loads -->
    <a class="link">some button</a>  <!-- this is present when the page loads -->


    <a class="link">some button</a>  <!-- this one is dynamic -->
    <a class="link">some button</a>  <!-- this one is dynamic -->
    <a class="link">some button</a>  <!-- this one is dynamic -->

    <span>some text</span>  <!-- this one won't match the selector -->
    <span>some text</span>  <!-- this one won't match the selector -->

</div>

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.

$('#some_container').delegate('a.link', 'click', function() {
    // runs your code when an "a.link" inside of "some_container" is clicked
});

So you can see that it doesn't matter when the "a.link" elements are added to the DOM, as long as the some_container existed when the page loaded.

The live()[docs] method is the same, except that the container is the document, so it processes all clicks on the page.

$('a.link').live('click',function() {
    // runs your code when any "a.link" is clicked
});
傾旎 2024-11-23 00:46:09

然后您将需要使用.live()。看看 http://api.jquery.com/live/

示例:

$('a').live('click', function() {
  // Do something useful on click.
});

在上面的示例中,任何 A 元素,无论是已经存在的还是在文档加载后加载的,都会触发 click 事件。

Then you'll want to use .live(). Have a look at http://api.jquery.com/live/.

Example:

$('a').live('click', function() {
  // Do something useful on click.
});

In the example above, any A elements, whether already existing or loaded after the document is loaded, will trigger the click event.

朮生 2024-11-23 00:46:09

这些答案现在已过时,因为自 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()

み零 2024-11-23 00:46:09

使用 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文