【问题标题】:detect barcode reader input检测条形码阅读器输入
【发布时间】:2012-03-02 08:46:37
【问题描述】:

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

$("#barcode_form").click(function(){
  var focusedElement = "";
  $(":focus").each(function(){  
    focusedElement = $(this).attr("id")                
  });
  if(focusedElement == ""){
    $('#text2display').focus()
  }
})

【问题讨论】:

  • 这将在什么浏览器中运行?是在移动设备上还是桌面上?

标签: javascript jquery barcode


【解决方案1】:

你会想要 .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;
}

这应该很适合你。

【讨论】:

  • 感谢您的回复 Ohgodwhy,但似乎无法正常工作。此外,我正在寻找 "$("#barcode_form").click(function()" 的替代品,因为我想在弹出窗口打开时检测来自条形码的任何输入
  • $("#barcode_form").click(function() { var focusElement = ""; $(":focus").each(function() {focusedElement = $(this).attr ("id") }); if(focusedElement == "") { $('#text2display').live('blur', function() { $("#text2display").focus() }); } })
  • 其实已经很晚了,我可能已经完成了对原始问题中您的需求的误解。
猜你喜欢
  • 2012-01-27
  • 1970-01-01
  • 2013-06-30
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多