bind()方法绑定事件的时候,第二个参数是函数,如果代码都写在函数里面,没有任何问题。但是,直接调用外部封装的函数需要注意,出错的例子:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<div>点我</div>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$("div").bind("click",popWindow());
//弹窗测试函数
function popWindow(){
alert("弹窗方法执行了...")
} </script>
</body>
</html>

  

上面这个例子打开网页立即弹窗,说明外部的函数立马执行了。当我们点击div的时候,没反应了。。。

在匿名函数里面调用外面封装的函数就没有问题了,正确的例子:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<div>点我</div>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$("div").bind("click",function(){popWindow()});
//弹窗测试函数
function popWindow(){
alert("弹窗方法执行了...")
} </script>
</body>
</html>

这样就正常了。

我们在实际操作中,很可能绑定好多个函数,这个函数有的要求执行1次便需要解绑,有的需要一直绑定。这种情况下,错误的例子:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<div>点我</div>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$("div").bind("click",function(){popWindow1()});
$("div").bind("click",function(){popWindow2();$(this).unbind("click");});
//弹窗测试函数1
function popWindow1(){
alert("弹窗方法1执行了...")
}
//弹窗测试函数2
function popWindow2(){
alert("弹窗方法2执行了...")
} </script>
</body>
</html>

这样操作,点击1次后,其实是把click事件解除了。之后,当然popWindow1()和popWindow2()都不会执行了。如果我们需要popWindow2()点击一次后消失,而popWindow1()点击时一直存在。这种写法就不妥了。对于这种需求,正确的例子:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<div>点我</div>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$("div").bind("click",function(){popWindow1()});
$("div").bind("click",function(event){popWindow2();$(this).unbind(event);});
//弹窗测试函数1
function popWindow1(){
alert("弹窗方法1执行了...")
}
//弹窗测试函数2
function popWindow2(){
alert("弹窗方法2执行了...")
} </script>
</body>
</html>

这样,点击一次后,popWindow2()消失,而popWindow1()点击时一直存在。$(this).unbind(event) 把本次绑定的事件取消了,而不会取消之前绑定过的事件,灵活性增加了。比如前面有个条件,如果达到这个条件后,取消绑定的这个函数,就用这个方法。

当然,上面的这个例子比较简单,第二个函数可以用jquery中的one()方法绑定,点击一次后解绑实现同样的功能。

注意:unbind()去不掉行间事件

Jquery还有许多这样的例子,都是一样的解决办法。

最新文章

  1. SQLBackupAndFTP The server principal &quot;NT AUTHORITY\SYSTEM&quot; is not able to access the database &quot;xxxx&quot;
  2. Java 中 手动抛出异常: throw new Exception(&quot;错误信息&quot;) 错误信息的获得
  3. A log about Reading the memroy of Other Process in C++/WIN API--ReadProcessMemory()
  4. 操作系统开发系列—解释typedef void (*int_handler) ();
  5. Java知多少(109)数据库更新
  6. !!转!!java 简单工厂模式
  7. 关于doctype
  8. 基于s5pv210嵌入式系统busybox文件系统移植
  9. 转自微软内部资料:编写高性能 Web 应用程序的 10 个技巧
  10. android 手势滑动
  11. eclipse weblogic debug 简易配置版
  12. php Redis常用命令
  13. JavaScript练习笔记整理&#183;3 - 6.25
  14. PAT1001 A+B Format
  15. Microsoft Power BI 学习笔记
  16. 使用Postman测试https接口时的小问题记录
  17. Python全栈学习_作业集锦(持续更新)
  18. navicat for mysql 只把指定的表数据结构导出
  19. srs2.0安装问题
  20. Go语言安全编码规范-翻译(分享转发)

热门文章

  1. Java异常以及继承的一些问题
  2. 基于Homestead搭建PHP项目开发环境(适合Zend Framework,Laravel,Yii,thinkphp等)
  3. 如鹏网JAVA培训笔记1(晓伟整理)
  4. Drop a database in MongoDB
  5. Java - 将vCard中十六进制编码转换成Unicode
  6. 【ACM】hdu_zs2_1006_Problem F_201308031058
  7. 洛谷——P1361 小猫爬山
  8. 先验概率 vs 后验概率
  9. SQL Server 运行计划操作符具体解释(2)——串联(Concatenation )
  10. #leetcode#Anagrames