要检查jQuery中元素上是否存在事件,请检查元素上是否存在现有事件。
在这里,我设置了div-
<div id="demo"> This is demo text. Click here! </div>
当您单击div时,警报将生成使用div ID设置的警报:
$("#demo").click(function() { alert("Does event exists? - "+hasEvents); });
您可以尝试运行以下代码来检查事件是否存在-
<!DOCTYPE html> <html> <head> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#demo").click(function() { alert("Does event exists? - "+hasEvents); }); var events = $._data(document.getElementById('demo'), "events"); var hasEvents = (events != null); }); </script> </head> <body> <div id="demo">This is demo text. Click here!</div> </body> </html>