JQuery 3.4.1 无法读取未定义的属性“值”

Unk*_*123 5 javascript jquery events upgrade

将 jQuery 3.4.0 升级到 3.4.1 后,在输入blur()focus()事件上Cannot read property 'value' of undefined出现错误。

that.container.on('click', '.binary_field', function(){
    $("input:focus").blur();
}
Run Code Online (Sandbox Code Playgroud)

我认为这可能是 jquery 源代码发生这种变化的原因:https://github.com/jquery/jquery/commit/24d71ac70406f522fc1b09bf7c4025251ec3aee6?diff=unified#diff-031bb62d959e7e4949d15537f3R

R3t*_*tep 5

你的代码看起来不错,你的情况有一个解决方法,删除这一行的jquery。

您可以更换

$("input:focus").blur()
Run Code Online (Sandbox Code Playgroud)

经过

document.activeElement && document.activeElement.blur() 
// Test if there is a focused element, if yes remove the focus
Run Code Online (Sandbox Code Playgroud)

或(使用 jQuery 和 VanillaJs)

$(document.activeElement)[0].blur()
// or
$("input:focus")[0].blur()
Run Code Online (Sandbox Code Playgroud)

这是同样的行为


小智 4

这是 jQuery 3.4.1 中的一个已知问题:

https://github.com/jquery/jquery/issues/4417

使用 JavaScript .blur() 而不是 jQuery 方法。