码迷,mamicode.com
首页 > Web开发 > 详细

jQuery源码解析之on事件绑定

时间:2019-09-21 23:10:18      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:appendto   test   UNC   ali   timeout   his   ddd   参数   nts   

jquery的on方法用来在选定的元素上绑定一个或多个事件处理函数。

当参数selector存在时,通常会用来对已经存在的元素或将来即将添加到文档中的元素做事件委托,表示当点击document中的selector元素时,将触发function回调函数。

 



1 <div id="div" style="font-weight:800;font-size:24px;text-align:center;color:red;"> 2 <p id="paragraph">test events </p> 3 </div> 4 5 <script src="../jquery-3.2.1.js"></script> 6 <script> 7 $(function(){ 8 $(document).on("click","#paragraph",function(){ 9 console.log(‘this is paragraph‘); 10 }); 11 $(document).on("click","#div",function(){ 12 console.log(‘this is div‘); 13 }); 14 $(document).on("click","#addDiv",function(){ 15 console.log(‘this is add div‘); 16 }); 17 18 setTimeout(function(){ 19 createDiv(); 20 },2000); 21 }); 22 23 function createDiv(){ 24 $("<div>").attr("id","addDiv").html("this is a div add after event binding").appendTo($("body")); 25 } 26 </script>

 

jQuery源码解析之on事件绑定

标签:appendto   test   UNC   ali   timeout   his   ddd   参数   nts   

原文地址:https://www.cnblogs.com/shun1015/p/11564995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!