在ajax加载的内容上重新运行应用程序Javascript [英] Re-Run application Javascript on ajax loaded content

查看:64
本文介绍了在ajax加载的内容上重新运行应用程序Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望应用程序JQuery脚本可以评估应用程序中所有AJAX加载的内容,这与普通加载的内容相同.例如JQuery会扫描AJAX加载的内容以查找选择器,例如模式框链接"等.

I would like all AJAX loaded content in my app to be evaluated by my application JQuery script, the same as normal loaded content is. e.g. JQuery scans through the AJAX loaded content for selectors, like 'modal box links' etc.

我所有的JavaScript都在常规document.ready中,对于正常的HTTP加载页面而言,它工作正常:

All my JavaScript is in the normal document.ready, which works fine for normal HTTP loaded pages:

$(document).ready(function(){
  // my apps javascript
});

我想使用.ajaxComplete之类的东西来重新运行document.ready中的所有内容.准备为jquery选择器评估新加载的AJAX内容.

I would like to use something like .ajaxComplete to fire a re-run of everything contained in document.ready to evaluate newly loaded AJAX content for jquery selectors.

$(document).ajaxComplete(function(){
  // Re-run all my apps javascript
})

是否可以在.ajaxComplete中放入一些代码来做到这一点?

Is there some code I can put in .ajaxComplete to do this?

希望这是有道理的,如果没有请告诉我,我将编辑问题详细信息.

Hope this makes sense, please let me know if it doesn't, and I will edit question details.

推荐答案

您可以将document.ready中的所有内容封装到一个函数中,然后再次调用该函数以重新绑定

you could encapsulate everything in your document.ready into a function and just call that function again to re-bind

或...

一种更好的方法是利用live()delegate() jQuery方法,以便与这些选择器匹配的所有当前和将来元素也将绑定到这些事件.

a better approach would be to take advantage of the live() and delegate() jQuery methods so that all current and future elements matching those selectors will be bound to these events as well.

使用live()的示例: http://api.jquery.com/live/

$('.clickme').live('click', function() {
    //all elements that exist now and elements added later
    //with this class have this click event attached
});

使用delegate()的示例: http://api.jquery.com/delegate/

$("table").delegate("td", "hover", function(){
    //all current and future td elements in all current tables
    //have this hover event attached
});

这篇关于在ajax加载的内容上重新运行应用程序Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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