可能重复:
How to debug Javascript/jQuery event bindings with FireBug (or similar tool)
How to find event listeners on a DOM node?
如何通过JavaScript检测哪些事件已绑定到DOM元素?在下面的示例中,我希望能够看到click事件已绑定在我的“可点击”范围内,并且如果可能,可以从alert
行进行调试。
我可以使用像Firebug这样的Web开发人员实用程序吗?
<html>
<head>
<title>Events bound with JavaScript</title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#clickable").click(function() {
alert("Clicked.");
});
});
</script>
</head>
<body>
<span id="clickable">Click me</span>
</body>
</html>
答案 0 :(得分:1)
// this function will return number of keys in object
function objectLenght (obj) {
var len = 0;
for (var key in obj) {
len++;
}
return len;
}
var objWithClickEvents = [];
// iterate through all elements
$('*').each(function() {
var clickEvents = $.data($(this)[0], 'events').click;
if (objectLength(clickEvents) > 0) {
objWithClickEvents[objWithClickEvents.length] = $(this);
}
});
如果您愿意,请将click
替换为其他活动。