https://www.cnblogs.com/sandraryan/
以前用的是bind(); 后來更新后用的on
(on() 方法是 bind()、live() 和 delegate() 方法的新的替代品)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div class="box1">hhhhhhhh</div> <div class="box2">hhhhhhhh</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script> <script> // bind(); 舊的綁定事件的方法 $('.box1').on('click mouseover mouseout',function(ev){ // var e = ev||event; console.log(233); }); // 添加不同事件,執行相同事件處理程序 $('.box2').on({ click: function(){ console.log('click'); }, mouseover : function(){ console.log('mouseover'); }, mouseout : function(){ console.log('mouseout'); } });
// 添加不同時間的不同執行程序
</script> </body> </html>
off 移除事件
on
給元素下子元素綁定事件
<div class="wrap"> <h2>this is h2</h2> <p>this is a para</p> <mark>mark text<mark> <div>div11111111</div> <div>div22222222</div> </div> $('.wrap').on('click','div',function(){ $(this).css({ fontSize: '40px', color :'red' }); }); // 把一個js原生對象作為實參傳到$函數中,就能拿到這個原生對象對應的jq對象, // 然后調用jq操作函數