我有一个问题,我正在使用 jquery U.I 选项卡,该选项卡通过 ajax 加载所有内容。现在,每次您单击选项卡时,我都会看到部分视图会加载到该选项卡中。
现在,在这个部分视图中,它们是 javascript 文件,它们使用 jquery 绑定该选项卡中所需的所有事件以及我正在使用的一些 jquery 插件。
现在,每次加载该选项卡时,所有这些脚本都会加载。如果单击 10 次,那么这些脚本将加载 10 次,这意味着现在每个按钮上都会有 10 个相同的事件,这意味着如果有人单击该按钮,10 个事件将全部触发并执行相同的操作。
所以我需要找到一些解决方案来将所有脚本移出并将其放在主页上并使用 jquery.live 或其他解决方案。
我尝试对 U.I 选项卡使用 jquery 缓存,但这不起作用,因为选项卡 A 中的某些内容更改后影响选项卡 B 意味着我需要重新加载选项卡 B,但脚本无法重新加载,否则我会遇到相同的问题问题和现在一样。
在这里阅读 http://ejohn.org/apps/workshop/adv-talk/index2.html#3精彩的演示涵盖了 jQuery 的事件委托。
看完上面的内容再看一下:
// using jQuery 1.4.2
(function($){
// You can cache the container you plan on using for event delegation.
// If using $(document).ready(...) to call the below functions
// your "container" element should already exist in the DOM
// (the .specialAjaxLink elements don't have to exist yet).
var container = $("#container");
// whenever an element with the class "specialAjaxLink" is clicked
// while inside your container element execute the handler on them.
container.delegate(".specialAjaxLink", "click", function(){
// do something amazing...
solveWorldHunger(this);
// stop propagation and prevent default...
return false;
});
}(jQuery));
使用上述方法时,您不必担心性能问题,除非您正在创建像 gmail、docs 或 google wave 这样复杂的应用程序。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)