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

时间:2022-06-01 19:13:25

I am currently testing this in Mozilla FireFox 3.0.5 using FireBug 1.3.0 with jQuery 1.2.6.

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

First try

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

FireBug console:

document.getElementById("helloworld").onfocus.toString() = function body as a string

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

$("#helloworld").get(0).onfocus.toString() = function body as a string

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


Second try

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

FireBug console:

document.getElementById("helloworld").onfocus.toString() = FireBug returns nothing

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

$("#helloworld").get(0).onfocus.toString() = FireBug returns nothing

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


What am I missing here? Why can't I find the callbacks when attaching them with jQuery?

我在这里想念的是什么?为什么我在使用jQuery附加回调时找不到回调?

2 个解决方案

#1


To view events that jQuery has bound use :

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

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

If you bind the focus as per your example and you run the above in firebug console it will return

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

Object focus=Object

#2


jQuery doesn't attach the callbacks directly, instead it stores them internally in a registry. Whenever an event is triggered, jQuery looks in the registry and calls the callback that you asked for earlier.

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

This gives you the advantage of being able to stack multiple callbacks onto a single element's event, but it has the disadvantage that you must use jQuery's event handler routines to set, get, and remove the callbacks.

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

#1


To view events that jQuery has bound use :

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

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

If you bind the focus as per your example and you run the above in firebug console it will return

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

Object focus=Object

#2


jQuery doesn't attach the callbacks directly, instead it stores them internally in a registry. Whenever an event is triggered, jQuery looks in the registry and calls the callback that you asked for earlier.

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

This gives you the advantage of being able to stack multiple callbacks onto a single element's event, but it has the disadvantage that you must use jQuery's event handler routines to set, get, and remove the callbacks.

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