取消方法:1、利用off()函數(shù),該函數(shù)可移除通過on()添加的事件處理程序,語法“綁定了事件的元素.off()”;2、利用unbind()函數(shù),該函數(shù)可刪除任意jq方法添加的事件處理程序,語法“綁定了事件的元素.unbind();”。
本教程操作環(huán)境:windows7系統(tǒng)、jquery1.10.2版本、Dell G3電腦。
jquery取消on()綁定的事件
方法1:使用off() 函數(shù)
off() 函數(shù)通常用于移除通過 on() 方法添加的事件處理程序。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function() { $("p").on("click", function() { $(this).slideToggle(); }); $("button").click(function() { $("p").off(); }); }); </script> </head> <body> <p>這是一個段落。</p> <p>這是另一個段落。</p> <p>點擊任何段落可以令其消失。包括本段落。</p> <button>移除on()添加的事件</button> </body> </html>
2、使用unbind() 函數(shù)
unbind() 函數(shù)移除被選元素的事件處理程序。
ubind() 適用于任何通過 jQuery 附加的事件處理程序。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function() { $("p").on("click", function() { $(this).slideToggle(); }); $("button").click(function() { $("p").unbind(); }); }); </script> </head> <body> <p>這是一個段落。</p> <p>這是另一個段落。</p> <p>點擊任何段落可以令其消失。包括本段落。</p> <button>移除 on()添加的事件</button> </body> </html>
【推薦學(xué)習(xí):jQuery視頻教程、web前端視頻】