2

我目前正在使用 FireBug 1.3.0 和 jQuery 1.2.6 在 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 附加回调时找不到回调?

4

2 回答 2

6

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

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

如果您根据示例绑定焦点并在 firebug 控制台中运行上述内容,它将返回

Object focus=Object
于 2009-01-14T12:51:53.037 回答
4

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

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

于 2009-01-14T12:25:50.797 回答