本文介绍了如何有效地使用yepnope.js和$(document).ready()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在实现yepnope脚本加载器作为modernizr.js库的一部分。之后我成功地加载了jQuery以加载jQuery依赖脚本。我是异步加载资源的新手,所以对我来说这有点新鲜。我一直在寻找,但是下面没有太多运气。

I have been implementing the yepnope script loader as part of the modernizr.js library. I have successfully got jQuery to load and jQuery dependent scripts afterwards. I am new to asynchronous loading of resources, so it's a bit new to me. I have been searching around, but haven't had much luck with the following.

我的问题是你对如何有效替换$的功能有何看法(文件).ready()在使用yepnope.js框架时。

My question is what are your opinions on how to effectively replace the functionality of $(document).ready() when working with the yepnope.js framework.

我的理论是在我的基础库中创建一个适当命名的函数,然后在我的页面上将该变量设置为包含我现有的$(document).ready的匿名函数( )代码。在所有脚本加载到完整回调之后,yepnope将调用此变量。

My theory was to create a appropriately named function in my base library and then set that variable on my pages to an anonymous function containing my existing $(document).ready() code. This variable would then be called by yepnope after all the scripts had loaded in the complete callback.

您是否同意这是一个很好的方法,或者我是完全以错误的方式接近这个?

Would you agree that this is a good way of doing it, or am I approaching this entirely the wrong way?

(对于那些不知道,yepnope.js的异步性质意味着文档在yepnope加载器完成之前调用$或jQuery,抛出一个$未定义错误< - 如果错误请纠正我。)

(For those unaware, the asynchronous nature of yepnope.js means that the document calls $ or jQuery before the yepnope loader has finished, throwing a "$ is undefined" error <- please correct me if that is wrong.)

第一个问题,希望它是一个好的。

First question, hope it's a good one.

推荐答案

如果没有yepnope加载jQuery对你来说不是问题,那么有一种更简单的方法。

If load jQuery without yepnope isn't a problem for you, there is a easier way to do.

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script>
    $.holdReady(true);

    yepnope.load({
        load: [
            'placeholder.js',
            'jquery-ui.min.js'
        ],
        complete: function (){
            $.holdReady(false);
        }
    });
</script>

这篇关于如何有效地使用yepnope.js和$(document).ready()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 00:31