DOM 在加载函数后不响应 jquery
我有一个动态加载内容的简单页面。 我使用 jQuery 加载函数将内容附加到 div。 load 函数从另一个文件加载内容。 我遇到的问题是,内容加载后,我构建的 jQuery 函数不响应单击事件。
为了证明它可以正常工作,我包含了内容,并且在 DOM 加载之后它可以完美工作。需要帮助请。
I have a simple page that loads content dynamically.
I use the jQuery load function to append content to a div. The load function loads content from another file.
The problem I am having is that after the content loads, the jQuery functions I have built are not responding to click events.
To prove that it works otherwise, I include the content and after DOM loads it works perfectly. Need help please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当你执行这样的操作时:
...只有 DOM 中已经存在的元素才会被选择器匹配并挂接其点击事件;如果您稍后添加将与选择器匹配的元素,则事后它们将不会自动连接。
您可以使用事件委托来使用
delegate
或(使用较新版本的 jQuery)on
变体之一:(请注意顺序两者之间的参数略有不同。)
对于事件委托,什么真正发生的是 jQuery 挂钩
click
容器,当单击冒泡到容器时,它会检查事件所采取的路径,以查看是否有任何元素- Between 匹配选择器。如果是这样,它会触发处理程序,就像您将其挂接到元素上一样,即使它实际上挂在容器上。具体示例:
如果我们这样做:
...然后单击其中一个跨度将显示其内容,如果我们这样做:
...该跨度将被自动处理,因为事件处理程序位于容器< /em> 然后选择器在事件发生时进行检查,因此我们稍后添加该元素并不重要。
When you do something like this:
...only elements that are already in the DOM will be matched by the selector and have their click event hooked up; if you add elements later that would have matched the selector, they will not be automatically hooked up after-the-fact.
You can use event delegation to seemingly hook them up automatically using
delegate
or (with newer versions of jQuery) one of theon
variants:(Note that the order of arguments is slightly different between the two.)
With event delegation, what really happens is that jQuery hooks
click
on the container, and when the click bubbles up to the container, it checks the path the event took to see if any of the elements in-between matched the selector. If so, it fires the handler as though you'd hooked it to the element, even though it's actually hooked on the container.Concrete example:
If we do this:
...then clicking one of the spans will show its contents, and if we do this:
...that span will get handled automatically, because the event handler is on the container and then the selector checked when the event occurs, so it doesn't matter that we added the element later.
正如您在 jQuery 文档 中看到的:
通常,当您添加点击(或其他事件)使用 $('a').click(fn) 处理所有链接,您会发现事件在之后不再起作用您已使用 AJAX 请求将新内容加载到页面中。
当您调用 $('a') 时,它会返回调用时页面上的所有链接,并且 .click(fn) 仅将处理程序添加到这些元素。添加新链接时,它们不受影响。有关更长的讨论,请参阅 AJAX 和事件教程。
有两种方法可以处理此问题:
使用事件委托
事件委托是一种利用事件冒泡来捕获 DOM 中任何位置元素上的事件的技术。
从 jQuery 1.3 开始,您可以使用 live 和 die 方法对事件类型的子集进行事件委托。从 jQuery 1.4 开始,您可以使用这些方法(以及从 1.4.2 开始的委托和取消委托)对几乎任何事件类型进行事件委托。
对于早期版本的 jQuery,请查看 Brandon Aaron 的 Live Query 插件。您还可以通过绑定到公共容器并从那里侦听事件来手动处理事件委托。例如:
此示例将处理 #mydiv 中任何元素的点击,即使在添加点击处理程序时这些元素尚不存在。
使用事件重新绑定
此方法要求您在添加新元素时调用绑定方法。例如:
As you can see on jQuery's documentation:
Frequently, when you've added a click (or other event) handler to all links using $('a').click(fn), you'll find that the events no longer work after you've loaded new content into a page using an AJAX request.
When you call $('a'), it returns all the links on the page at the time it was called, and .click(fn) adds your handler to only those elements. When new links are added, they are not affected. See the AJAX and Events Tutorial for a longer discussion.
You have two ways of handling this:
Using event delegation
Event delegation is a technique that exploits event bubbling to capture events on elements anywhere in the DOM.
As of jQuery 1.3, you can use the live and die methods for event delegation with a subset of event types. As of jQuery 1.4, you can use these methods (along with delegate and undelegate starting in 1.4.2) for event delegation with pretty much any event type.
For earlier versions of jQuery, take a look at the Live Query plugin by Brandon Aaron. You may also manually handle event delegation by binding to a common container and listening for events from there. For example:
This example will handle clicks on any element within #mydiv, even if they do not exist yet when the click handler is added.
Using event rebinding
This method requires you to call the bind method on new elements as they are added. For example:
因为您是在 DOM 完全加载后插入内容,所以您必须使用
.on("click",function(){})
来实现点击功能,因为在绑定时它们在 DOM 中不可用!Because you are inserting the content AFTER the DOM has been fully loaded, you have to use
.on("click",function(){})
to achieve your click functionality because at the time you are binding them they are not available in the DOM !