对焦选择器不起作用

进入文本字段并输入一个字符。 调试器显示:

$( document.activeElement ) => work, node found + attribute you can read $(":focus").css("background-color","red") => work $( ':focus' ) => not work, no node found 

为什么? 有解决方案?

在API文档中,它表示$(document.activeElement)是$(’:focus’)。 只有性能差异。

见: http : //api.jquery.com/focus-selector/

谢谢!

   focus   $(function() { $(document).keypress(function(event) { $(":focus").css("background-color","red"); // <-- work var f3 = $( ':focus' ); // <-- NOT work var attr3 = f3.attr('tabindex'); var id3 = f3.attr('id'); f3.val(id3); var f1 = $( document.activeElement ); // <-- work var attr1 = f1.attr('tabindex'); var id1 = f1.attr('id'); console.log(id1); return; }); });    

test focus