一个AJAX请求之后,重新加载javascript文件 [英] Reload javascript file after an AJAX request

查看:530
本文介绍了一个AJAX请求之后,重新加载javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在该请求,创造了新的元素不是由我的jQuery code中的事件处理程序的认可。

After the request, the new elements created are not recognized by the event handlers in my jQuery code.

有没有办法重新加载重新注册这些事件的文件?

Is there a way to reload the file to re-register these events?

推荐答案

我假定你的意思是你已经注册的已换成你的Ajax请求的结果没有闪光?元素事件

I'm assuming that you mean that events you've registered for elements that have been replaced by with the results of your ajax requests aren't firing?

使用 .live()(见 HTTP://api.jquery。 COM /活/ 的),以对元件注册该事件,该匹配选择器(包括来自AJAX的结果创建的新的DOM元素),而不是选择器的结果时,该事件处理程序是第一,这被替换的时候,他们将被销毁。

Use .live() (see http://api.jquery.com/live/) to register the events against elements that the match the selector (including the new DOM elements created from the results of the ajax), rather than the results of the selector when the event handlers were first, which will be destroyed when they are replaced.

例如。 替换

$('div.someClass').click(function(e){
    //do stuff
});

$('div.someClass').live('click', function(e){
    //do stuff
});

重要提示:

虽然我也推荐使用 .live()这是为了清楚起见,其语法类似于 .bi​​nd(),你应该使用。对()如果可能的话。见@ jbabey的评论对重要信息的链接。

While I've recommended using .live() this is for clarity as its syntax is similar to .bind(), you should use .on() if possible. See links in @jbabey's comment for important information.

这篇关于一个AJAX请求之后,重新加载javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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