简体   繁体   English

在 jQuery 输入字段事件处理中链接焦点()和模糊()?

[英]Chaining focus( ) and blur( ) in jQuery input field event processing?

In jQuery i've seen the blur( ) chained to the focus( ) like this:在 jQuery 中,我看到 blur() 链接到 focus(),如下所示:

$("input").focus(function(){
    $(this).css("background-color", "blue");
}).blur(function(){
    $(this).css("background-color", "red");
});

Why doesn't blur( ) execute immediately after the focus event is fired?为什么在触发焦点事件后不立即执行 blur()? When blur( ) is chained to focus( ) it doesn't execute until after the field loses focus yet because it is chained I would think it would fire immediately after the focus event is fired even though the input field has not yet lost focus.当 blur() 链接到 focus() 时,它直到字段失去焦点后才会执行,因为它是链接的,我认为即使输入字段尚未失去焦点,它也会在触发焦点事件后立即触发。

when chaining non-event functions they always execute in quick succession当链接非事件函数时,它们总是快速连续执行

In this case, they functions are running immediately, but these functions are event bindings , not the events .在这种情况下,它们的函数立即运行,但这些函数是事件绑定,而不是事件

So the blur event is not chained to the focus event, only the event handlers are added via chaining.所以模糊事件没有链接到焦点事件,只有事件处理程序通过链接添加。


Note that chaining just allows for functions to be called one after the other without interim variables.请注意,链接只允许在没有中间变量的情况下一个接一个地调用函数。 There's no difference in your code from:您的代码与以下没有区别:

var inp = $("input");
inp.focus(function() { $(this).css("background-color", "blue"); });
inp.blur(function() { $(this).css("background-color", "red"); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM