jQuery.holdReady()方法用法实例


本文实例讲述了jQuery.holdReady()方法用法。分享给大家供大家参考。具体分析如下:

此方法可以暂停或者恢复jQuery.ready()事件。
调用此方法可以延迟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.phpstudy.net/" />
<title>phpstudy</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.phpstudy.net/" />
<title>phpstudy</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>

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

希望本文所述对大家的jQuery程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3