Jquery 事件在 ajax/innerHtml 之后不起作用
我有很多事件,例如
$('.hidefile').click(function (event) {
页面加载,并且在某些条件下我使用ajax来获取特定div的html并用.html(htmlstring)填充它。我花了很长时间,但我注意到当我这样做时,这些事件不起作用。我如何用 html 填充 div 并让 jquery 事件与其一起工作?
i have many events such as
$('.hidefile').click(function (event) {
On page load and on certain conditions i use ajax to get the html for a certain div and fill it in with .html(htmlstring). It took me forever but i notice that the events do not work when i do this. How do i fill the div with html and have the jquery events work with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将点击事件(或其他方式)绑定到元素:
使用
live
如果使用 ajax 替换元素,它们就会丢失事件处理程序。您可以在用于替换这些元素的特定 ajax 方法的success
回调中显式重新绑定它们,或者使用 live 处理程序,这将:Bind your click events (or otherwise) to your elements using
live
:When you replace elements using ajax, they lose their event handlers. You can either explicitly rebind them within the
success
callback of the particular ajax method you have used to replace those elements, or use live handlers instead, which will:你如何附加点击事件。您应该在 document.ready 事件中执行此操作,以确保 DOM 已加载。另外,正如 karim79 所建议的,您应该使用 live 功能来确保事件持续存在。
How are you attaching the click event. You should do that inside the document.ready event to make sure that the DOM has been loaded. Also, as suggested by karim79 you should use the live function to make sure that the events persists.