在DOM元素上查找使用jQuery添加的回调函数

时间:2009-01-14 11:58:04

标签: javascript jquery events callback

我目前正在使用带有jQuery 1.2.6的FireBug 1.3.0在Mozilla FireFox 3.0.5中进行测试。

首先尝试

document.getElementById("x").onfocus = function ()
{
    var helloWorld = "Hello World";
};

FireBug控制台:

document.getElementById(“helloworld”)。onfocus.toString() =函数体作为字符串

$(“#helloworld”)。get(0).onfocus.toString() =函数体作为字符串


第二次尝试

$("#helloworld").focus(function ()
{
    var helloWorld = "Hello World";
});

FireBug控制台:

document.getElementById(“helloworld”)。onfocus.toString() = FireBug什么都不返回

$(“#helloworld”)。get(0).onfocus.toString() = FireBug什么都不返回


我在这里缺少什么?为什么在用jQuery附加回调时我找不到回调?

2 个答案:

答案 0 :(得分:6)

要查看jQuery绑定使用的事件:

$("#helloworld").data('events');

如果按照示例绑定焦点,并在firebug控制台中运行上面的内容,它将返回

Object focus=Object

答案 1 :(得分:4)

jQuery不直接附加回调,而是将它们内部存储在注册表中。每当触发事件时,jQuery会在注册表中查找并调用您之前要求的回调。

这使您能够将多个回调堆叠到单个元素的事件上,但它的缺点是必须使用jQuery的事件处理程序例程来设置,获取和删除回调