你會想要的.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;
}
這應該爲你工作。
這是什麼瀏覽器運行?它是在移動設備上還是在桌面上? – 2012-03-02 11:38:09