Jquery-core.holdReady()

jQuery.holdReady()

Description: Holds or releases the execution of jQuery's readyevent.

描述:锁定或者释放jQuery的完成事件。

version added: 1.6jQuery.holdReady( hold )

1.6版本的jQuery 加入该函数

 

jQuery.holdReady(hold) whether the ready hold is being requestedor released

The $.holdReady() method allows the caller to delay jQuery'sready event. This advanced feature would typically be used by dynamic scriptloaders that want to load additional JavaScript such as jQuery plugins beforeallowing the ready event to occur, even though the DOM may be ready. Thismethod must be called early in the document, such as in the <head>immediately after the jQuery script tag. Calling this method after the readyevent has already fired will have no effect.

To delay the ready event, first call $.holdReady(true). When theready event should be released to execute, call $.holdReady(false). Note thatmultiple holds can be put on the ready event, one for each $.holdReady(true)call. The ready event will not actually fire until all holds have been releasedwith a corresponding number of $.holdReady(false) calls and the normal documentready conditions are met. (See ready for more information.)

 

jQuery.holdReady(hold) hold表示完成事件的状态(被锁定或释放)
$.holdReady()
方法允许jQuery的完成事件被该函数锁定。这个高级特性的典型应用场合是动态载入脚本,比如jQuery插件等。在附加的脚本载入完成前,即使页面已经准备好,jQuery的完成事件也不会被触发。这个函数必须在页面的靠前部分被调用,比如在<head>标签当中,jQuery载入下一行。在完成事件被触发后调用此函数没有任何效果。

使用方法:首先调用$.holdReady(true)[调用后完成事件将被锁定]。当准备好触发完成事件时,调用$.holdReady(false)。需要注意的是,可以对完成事件添加多个锁定,每个锁定对应一次$.holdReady(false)[解锁]调用。jQuery的完成事件将在所有的锁定都被解除,并且页面也已经准备好的情况下被触发。

Example:

Delay the ready event until acustom plugin has loaded.

例子:
锁定完成事件直到用户脚本[myplugin.js]载入完成


$.holdReady(true);
$.getScript("myplugin.js", function() {
    
$.holdReady(false);
});

以上是现成度娘来的资料,以下详细解释一些其他的试验代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"  ></script>
    <script type="text/javascript">
        $(function () { alert($("#div_test").html()); })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="div_test">
      this is a test!
    </div>
    </form>
</body>
</html>


 

该代码的运行结果是 跳出this is a test! 正常运行,现在讲代码改如下:

<script type="text/javascript">
        $.holdReady(true);
        $(function () { alert($("#div_test").html()); })
    </script>

代码将不会跳出弹出窗口。因为$.holdReady(true);锁定了Jquery对象。再后面加上$.holdReady(false);释放代码就恢复正常。

 

<script type="text/javascript">
        $.holdReady(true);
        $(function () { alert($("#div_test").html()); })
        $.holdReady(false);
    </script>


 


 

猜你喜欢

转载自blog.csdn.net/wujiang1984/article/details/7571394