jQuery live会减慢网站速度吗? [英] Does jquery live slow down websites?

查看:80
本文介绍了jQuery live会减慢网站速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用jquery U.I选项卡加载ajax的所有内容时遇到问题.现在,每当您单击选项卡时,我都拥有它,部分视图会加载到该选项卡中.

I have a problem I am using jquery U.I tabs that load everything with ajax. Now I have it right now everytime you click on a tab a partial view is loaded up into that tab.

现在在此局部视图中,它们是使用jquery绑定该选项卡中所需的所有事件以及我正在使用的jquery插件的javascript文件.

Now in this partial view their are javascript files that use jquery to bind all the events that are needed in that tab plus some jquery plugins I am using.

现在,每次加载选项卡时,所有那些脚本都将加载.如果单击10次,则这些脚本将被加载10次,此时每个我的按钮现在将具有10个相同的事件,这意味着如果有人单击该按钮,则10个事件将全部触发并执行相同的操作.

Now every time that tab is loaded up all those scripts are loaded up. If it is clicked 10 times then those scripts are loaded 10times up meaing now each of say my buttons will now have 10 of the same events on it mean if someone clicks on that button 10 events will all fire off to and do the same thing.

因此,我需要找到一些解决方案以将所有脚本移出并放在主页上,并使用jquery.live或其他解决方案.

So I need to find some solution to either move all the script out and have it on the main page and use jquery.live or some other solution.

我尝试对UI选项卡使用jquery缓存,但是由于更改效果选项卡B时说选项卡A中的某些内容,这无法正常工作,这意味着我需要重新加载选项卡B,但无法重新加载脚本,否则我将运行陷入与现在相同的问题.

I tried to do use jquery caching for the U.I tabs but this won't work since some things in say Tab A when changed effect Tab B meaning I need Tab B to be reloaded but the scripts can't reload otherwise I run into the same problem as now.

推荐答案

在此处阅读,获得有关jQuery事件委托的精彩演示.

Read here for a nice presentation covering jQuery's event delegation.

阅读上面的内容后,请看以下内容:

After reading the above look at this:

// 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这样复杂的应用程序.

You shouldn't have to worry about performance problems when using the above method unless you are creating an app as complex as gmail, docs, or google wave.

这篇关于jQuery live会减慢网站速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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