JQuery1.8 判断元素是否绑定事件的方法教程
程序员文章站
2023-11-09 23:18:34
on previous versions, you could call it like for other data :
obj.data('events');...
on previous versions, you could call it like for other data :
obj.data('events');
in jquery 1.8, this direct access was removed, so in recent versions you must call it like this :
$._data(obj[0],"events")
大概的意思是版本可以使用obj.data('event'); jquery1.8版本取消了obj.data方法,改为$._data方法
注意:$._data(obj[0],"event") 中的obj[0],一定要加上数组[0]下标,否则会取不到数据
-------以下为举例
<html xmlns="https://www.w3.org/1999/xhtml"> <head> <title></title> <script src="/jquery-easyui-1.3.2/jquery-1.8.0.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("#btntest").click(function () { alert('aa'); }); $("#btn").click(function () { //判断是否绑定了click事件 var objevt = $._data($("#btntest")[0], "events"); if (objevt && objevt["click"]) { //console.info(objevt["click"]); alert("bind click"); } else { alert("not bind click"); } }); }); </script> </head> <body> <input type="button" id="btn" value="测试是否绑定事件" /> <input type="button" id="btntest" value="被测试按钮" /> </body> </html>