4

我正在使用 yepnope.js,但在加载函数时遇到了一个小问题

在头部我包括 yep nope 并调用相关的 js 文件

<script type="text/javascript" src="/code/trunk/javascript/external/modernizr/modernizr-development.js"></script>

<script type="text/javascript" src="/code/trunk/javascript/external/yepnope.1.5.3-min.js"></script>

<script type="text/javascript">
yepnope({
  test: Modernizr.mq('only all and (max-width: 700px)'),
  yep: ['/templates/client/jquery/qff/plugin.mobile.js'],
  nope:['/templates/client/jquery/qff/plugin.website.js']
});    
</script>

然后在页面底部

<script type="text/javascript">
jQuery(document).ready(function() 
{
    jQuery("#mainContent").setupQantas({
        startSlide: 1, 
        googleAnalytics:1,
        googleCode:""
    });
});
</script>

所以我在主屏幕上看这个。所以应该调用 plugin.mobile.js

在 plugin.mobile.js 文件中

(function( $ ){

  $.fn.setupQantas = function( options ) {  

    // Create some defaults, extending them with any options that were provided
    var settings = $.extend( {
        startSlide: 1, 
        googleAnalytics:0, // 1 sends to google
        googleCode: ""
    }, options);

    var methods = {};

    return this.each(function() {        

        if (settings.startSlide === 1) {
            alert("slide = 1");     
        } else {
            alert("slide > 1");
        }
    });
  };
})( jQuery );// JavaScript Document 

而不是给出警告幻灯片 1 它有错误

jQuery("#mainContent").setupQantas is not a function

如果我不使用 yepnope 而只是将它放在脚本标签中,它就可以工作。yepnope 在 js 文件中加载时似乎有延迟,并且在 doc.ready 之前似乎没有

有没有解决的办法?

谢谢

4

2 回答 2

3

是的,有延迟。这就是异步脚本加载器背后的全部意义所在。

您应该在 yepnope 加载脚本后使用回调。检查completecallback选项。

于 2013-03-01T04:39:46.823 回答
0

这是代码

<script type="text/javascript">

yepnope({
  test: Modernizr.mq('only all and (max-width: 700px)'),
  yep: ['/templates/client/jquery/qff/mobile.js'],
  nope:['/templates/client/jquery/qff/website.js'],
  complete: function () {
    jQuery("#mainContent").setupQantas({
        startSlide: 1, 
        googleAnalytics:1,
        googleCode:""
    });
  }
});


</script>
于 2013-03-01T04:56:15.697 回答