简体   繁体   English

如何按需加载jqtouch

[英]How to load jqtouch on-demand

I'm trying to load jqtouch on-demand like so: 我正在尝试按需加载jqtouch,如下所示:

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
    $(function() {
        $.getScript("js/jqtouch.min.js", function() {
             $.jQTouch();
        });
    });     
</script>

Firebug outputs: $(_3c.selector).tap is not a function Firebug输出:$(_ 3c.selector).tap不是函数

If I include jqtouch.min.js in a script, like I did for jquery.js and call $.jQtouch, everything will work correctly. 如果我在脚本中包含jqtouch.min.js,就像我为jquery.js所做的那样并调用$ .jQtouch,一切都会正常工作。 However, I'd like to load jqtouch only when I need to, however I can't seem to get it to work. 但是,我只想在需要的时候加载jqtouch,但是我似乎无法让它工作。 I also tried doing an ajax post to jqtouch.min.js and received the same error. 我也试过给jqtouch.min.js做一个ajax帖子并收到同样的错误。

When you load the jqtouch script on-demand, the $(document).ready event fires before the script is loaded, so the jqtouch initialization script is never executed. 当您按需加载jqtouch脚本时,$(document).ready事件在加载脚本之前触发,因此永远不会执行jqtouch初始化脚本。

The solution sucks....you have to modify the jqtouch script initialization binding from 解决方案很糟糕....你必须修改jqtouch脚本初始化绑定

$(document).ready(function(){...})

to

$(document).bind('ready',function(){...})

...and you have to modify your own code to fire the ready event after loading the script ...并且您必须修改自己的代码以在加载脚本后触发ready事件

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
    $(function() {
        $.getScript("js/jqtouch.min.js", function() {
             $(document).trigger('ready');
             $.jQTouch();
        });
    });     
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM