jQuery.holdReady()方法用法实例

调用此方法可以延迟jQuery的ready事件,也就是说尽管文档已经加载完成,也不会执行ready事件处理方法。
可以多次调用jQuery.holdReady()方法,以延迟jQuery的ready事件,当满足一定条件时,再通过将此方法的参数设置为false,一一解除延迟。方法一般用于动态脚本加载,知道脚本加载完成然后再通过将此方法的参数设置为false,解除对jQuery.ready()事件延迟。

语法结构:

jQuery.holdReady(hold)

参数列表:

参数

描述

hold

如果值为true,则会延迟jQuery.ready()事件。
如果值为false,则会解除对jQuery.ready()事件延迟。

如果值为false,则会解除对jQuery.ready()事件延迟。

实例代码:

实例一:

 

<!DOCTYPE html>

<html>

<head>

<meta charset=" utf-8">

<meta name="author" content="http://www.jb51.net/" />

<title>脚本之家</title>

<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>

<script type="text/javascript">

jQuery.holdReady(true);

$(document).ready(function(){

  alert("我不会被弹出");

})

</script>

</head>

<body>

  

</body>

</html>

在以上代码中,由于添加了 jQuery.holdReady(true),所以尽管文档加载完成,也不会执行ready()中的函数。

实例二:

<!DOCTYPE html>

<html>

<head>

<meta charset=" utf-8">

<meta name="author" content="http://www.jb51.net/" />

<title>脚本之家</title>

<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>

</head>

<body>

<button id="first">点击测试弹出</button>

<button id="second">解除延迟</button>

<script type="text/javascript">

jQuery.holdReady(true) 

$(document).ready(function(){

  $("#first").click(function(){

    alert("我不会被弹出");

  })

})

$("#second").click(function(){

  jQuery.holdReady(false);

})

</script>

</body>

</html>

当点击解除延迟之后,就可以弹出了。

 

posted @ 2016-07-16 15:08  每天进步多一点  阅读(1004)  评论(0编辑  收藏  举报