0

是否可以将元素传递给 Jquery 中的 focusout 函数?我想将所有文本框的焦点事件绑定在一个语句中。所以我的解决方案是让它们都属于同一个类,然后做类似的事情

$(".class-name").focusout( function() {
//do whatever
});

但是我想获得焦点内元素的值,而不必通过它的 id 来引用它,那么这样的事情可能吗?

$(".class-name").focusout( function(this) {
alert( $(this).val() );
});
4

3 回答 3

9

假设你意识到focusoutover的神奇本质blur这就是你想要的:

$("#the-common-parent-of-all-inputs").focusout( function(e) {
alert( e.target.value );
});

您只需要将一个绑定focusout到输入元素的公共父级。这是使用focusout.

您可能没有意识到,但是当您这样做时,$(".class-name").focusout它只会单独绑定到每个元素,这违背了focusout.

于 2011-11-30T21:36:10.133 回答
5

对于诸如 focusout (或任何其他 jQuery 事件)之类的事情,您可以执行以下操作:

$(".class-name").focusout( function() {
   //`this` refers to the object that was focusouted on
});

this总是指事件发生的对象

于 2011-11-30T21:18:14.470 回答
2

你试过了吗?它肯定可以工作,但没有(!)“this”参数。"this" 将自动引用当前元素,无需将其作为参数传递给函数。只是省略它。

于 2011-11-30T21:18:01.910 回答