jQuery中的事件: on()注册,off()删除,toggle()切换
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jQuery中的事件注册与删除: on()和off()方法的使用</title> </head> <style type="text/css"> .box{ width: 300px; height: 300px; margin: 0 auto; border: 1px solid green; } .box div{ width: 300px; height: 240px; text-align: center; } button{ background: #00b33b; border: 0; color: #fff; width: 120px; height: 30px; text-align: center; margin: 10px 13px; } button:hover{ opacity: 0.9; } .active{ color: #fff; } </style> <body> <div class="box"> <div> <p>toggleClass方法添加class</p> <h6 style="display: none;">toggle()方法:隐藏就显示,显示就隐藏</h6> </div> <button id="btn1">toggle/toggleClass</button> <button id="btn2">off()删除划出事件</button> </div> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script> //on()绑定事件 /*$('.box div').on('mouseenter',function(){ $(this).css('background','lightgray') }).on('mouseleave',function(){ $(this).css('background','lightcoral') })*/ //hover等价于mouseenter,mouseleave $('.box div').hover(function(){ $(this).css('background','lightgray') },function(){ $(this).css('background','lightcoral') }) //toggle() toggleClass() $('#btn1').click(function(){ $('.box div p').toggleClass('active'); $('.box div h6').toggle() }) //off() 删除事件 $('#btn2').click(function(){ $('.box div').off('mouseleave') }) </script> </body> </html>