Jquery 事件在 ajax/innerHtml 之后不起作用

发布于 2024-08-25 06:02:02 字数 212 浏览 4 评论 0原文

我有很多事件,例如

$('.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 技术交流群。

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

发布评论

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

评论(2

诗酒趁年少 2024-09-01 06:02:02

将点击事件(或其他方式)绑定到元素:

$('.hidefile').live("click", function (event) {...

使用 live 如果使用 ajax 替换元素,它们就会丢失事件处理程序。您可以在用于替换这些元素的特定 ajax 方法的 success 回调中显式重新绑定它们,或者使用 live 处理程序,这将:

将处理程序附加到所有事件
与当前匹配的元素
选择器,现在或将来。

Bind your click events (or otherwise) to your elements using live:

$('.hidefile').live("click", function (event) {...

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:

Attach a handler to the event for all
elements which match the current
selector, now or in the future.

不知在何时 2024-09-01 06:02:02

你如何附加点击事件。您应该在 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.

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