Javascript 检测条形码阅读器输入

Javascript 检测条形码阅读器输入,javascript,jquery,barcode,Javascript,Jquery,Barcode,下面是我的代码,如果单击表单上的任何位置(在弹出窗口中),我将重点关注“text2display”。但是,如果它们“tab”并导航到其他位置(例如浏览器url),如何检测到这一点并将焦点返回到“text2display” 您需要.blur();函数。每当元素具有“焦点”且用户离开“焦点”项时,就会调用“blur”,并将事件发送回该元素。下面是一个示例 if(focusedElement == ""){ //if our focused element is empty $('#text2

下面是我的代码,如果单击表单上的任何位置(在弹出窗口中),我将重点关注“text2display”。但是,如果它们“tab”并导航到其他位置(例如浏览器url),如何检测到这一点并将焦点返回到“text2display”


您需要.blur();函数。每当元素具有“焦点”且用户离开“焦点”项时,就会调用“blur”,并将事件发送回该元素。下面是一个示例

if(focusedElement == ""){
  //if our focused element is empty
  $('#text2display').live('blur', function(){
    //when the target loses focus, we invoke blur, the next line focuses again.
    //it's also a best practice to do something like fading in the border, 
    $("#text2display").focus();
  });
}
另外,如果我可以建议的话,最好让用户知道他们的注意力已经回到了该元素。最好的方法是添加一个“红色”边框,然后淡出该边框。一种方法如下->

if(focusedElement == ""){
  //if our focused element is empty
  $('#text2display').live('blur', function(){
    //when the target loses focus, we invoke blur, the next line focuses again.
    //it's also a best practice to do something like fading in the border, 
    $("#text2display").focus(function(){
      $(this).animate({
        //when the element has been re-focused, we'll give it a red border briefly.
        borderColor: '#ff0000';
      }, function(){
           //once the animation completes, fade the border out                    
           $(this).animate({ borderColor: 'transparent' }, 2000);
        }
      );
    });
  });
}
另外,因为我们不想调整大小,我们需要在输入中添加一些CSS

input{
  border:1px solid transparent;
}

这对您应该很有用。

谢谢您的回复,原因是,但似乎不起作用。此外,我正在寻找“$”(“#条形码_表单”)的替代品。单击(函数()”,因为我想在弹出窗口打开$(“#条形码_表单”)时检测条形码中的任何输入。单击(函数(){var focusedElement=”“;$(“:focus”).each(function(){focusedElement=$(this).attr(“id”)});if(focusedElement==”){$('#text2display').live('blur',function(){$(“#text2display”).focus()}}}实际上已经很晚了,我可能已经完全误解了您在原始问题中的需求。这将在哪个浏览器中运行?它是在移动设备上还是在桌面上?
input{
  border:1px solid transparent;
}