【发布时间】:2010-01-14 15:27:17
【问题描述】:
或者我怎么知道下面这个是否已经运行:
$target.bind('click',function() {
...
});
【问题讨论】:
-
您的意思是用于调试目的还是在运行时?
或者我怎么知道下面这个是否已经运行:
$target.bind('click',function() {
...
});
【问题讨论】:
我使用visual event bookmarklet 向我显示哪些项目有事件。
【讨论】:
借用和扩展大卫的例子,使用这个:
if(typeof $('#id').click == 'function') {}
【讨论】:
您可以访问jQuery.data 对象来查找:
$.fn.isBound = function() {
return this.length && typeof $.data(this[0], 'handle') == 'function' || false;
}
if ($target.isBound()) {
// $target has events
}
更新:如果您想检查特定类型,可以查看 events 对象:
$.fn.isBound = function(type) {
return this.length && $.data(this[0], 'events')[type] || false;
}
if ($target.isBound('click')) {
// $target has clickevents
}
【讨论】:
click handler 是否被绑定
您可以使用 $._data。 例如
$target.bind('click',function() {
...
});
$._data( $target[0], 'events'); // Object {click: Array[1]}
【讨论】: