jQuery,如何在.load()之后重新绑定html元素 [英] jQuery, how to rebind html-elements after .load()

查看:57
本文介绍了jQuery,如何在.load()之后重新绑定html元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html-component,将在单击按钮后重新加载. 该组件的某些元素绑定到单击和悬停处理程序. 一切正常,直到调用.load()函数重新加载组件为止. 加载的组件的元素未绑定到相应的处理程序.

I have a html-component that will be reload after a button has been clicked. Some elements of the component are bound to click and hover handlers. Everything is working fine until the .load()-function is called to reload the component. The elements of the loaded component are not bound to corresponding handlers.

这时,我将js脚本放在组件的末尾,以便它绑定元素. 但是,在组件重新发布之后,组件外部的元素现在不再绑定了.

At that point I put the js-script at the end of the component, so that it binds the elements. But after the components relaod the elemnts outside the component are unbound, now.

我已经用.on().live().bind().click()进行了尝试,...无济于事.

I've tried it with .on(), .live(), .bind(), .click(), ... nothing worked.

推荐答案

您应该能够调用.load()方法并创建一个可在其中重新绑定元素的回调.

You should be able to call the .load() method AND create a callback in which you can rebind the elements.

例如:

$(document).ready(function() {
    ...initially bind all event handlers for page (including elements to be reloaded)...

    $("#someButton").on("click", function() { 
        $("#elementToReload").load("page-to-load.html", function() {
             ...bind all event handlers for this and containing elements here...
        });
    });
});


如果对此有任何疑问,请告诉我.祝你好运! :)

Let me know if you have any questions on this. Good luck! :)

这篇关于jQuery,如何在.load()之后重新绑定html元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆