2012-03-02 98 views
0

这里是我的代码,如果点击窗体上的任何地方(弹出窗口),我的代码将专注于“text2display”。但是,怎么样,如果他们“标签”,并浏览其他地方(如浏览器URL),如何检测和焦点返回回“” text2display“检测条形码读取器输入

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

这是什么浏览器运行?它是在移动设备上还是在桌面上? – 2012-03-02 11:38:09

回答

0

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

这应该为你工作。

+0

感谢您的回复Ohgodwhy,但似乎没有工作。此外,我正在寻找替换“$(”#barcode_form“)。click(function()”,因为我想检测任何输入从条形码弹出时 – 2012-03-02 09:26:36

+0

btw,这是你的意思: – 2012-03-02 09:28:03

+0

$( “#barcode_form”)点击(函数() { VAR focusedElement = “”; $。( “:聚焦”)每个(函数() { focusedElement = $(本).attr( “ID” ) }); 如果(focusedElement == “”){ $( '#text2display')生活( '模糊',函数(){ $(“#text2display”)。focus() }); } }) – 2012-03-02 09:28:08