如何覆蓋 jQuery 事件處理器?
使用 off() 方法來覆蓋 jQuery 事件處理器。此方法用於移除事件處理器。on() 方法用於附加一個或多個事件處理器。
示例
您可以嘗試執行以下程式碼,瞭解如何覆蓋 jQuery 事件處理器 -
<html> <head> <title>jQuery off() method</title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function() { function aClick() { $("div").show().fadeOut("slow"); } $("#bind").click(function () { $("#theone").on("click", aClick).text("Can Click!"); }); $("#unbind").click(function () { $("#theone").off("click", aClick).text("Does nothing..."); }); }); </script> <style> button { margin:5px; } button#theone { color:red; background:yellow; } </style> </head> <body> <button id = "theone">Does nothing...</button> <button id = "bind">Bind Click</button> <button id = "unbind">Unbind Click</button> <div style = "display:none;">Click!</div> </body> </html>
廣告