Check if event exists on element
This question already has an answer here:
$('body').click(function(){ alert('test' )})
var foo = $.data( $('body').get(0), 'events' ).click
// you can query $.data( object, 'events' ) and get an object back, then see what events are attached to it.
$.each( foo, function(i,o) {
alert(i) // guid of the event
alert(o) // the function definition of the event handler
});
You can inspect by feeding the object reference ( not the jQuery object though ) to $.data, and for the second argument feed 'events' and that will return an object populated with all the events such as 'click'. You can loop through that object and see what the event handler does.
You may use:
$("#foo").unbind('click');
to make sure all click events are unbinded, then attach your event
以下是如何检查元素是否存在任何事件
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script>
$(function() {
$("#textDiv").click(function() {
//Event Handling
});
var events = $._data(document.getElementById('textDiv'), "events");
var hasEvents = (events != null);
});
</script>
</head>
<body>
<div id="textDiv">Text</div>
</body>
</html>
链接地址: http://www.djcxy.com/p/49170.html
下一篇: 检查元素上是否存在事件