2012-03-02 41 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