簡體   English   中英

jquery焦點不工作模糊

[英]jquery focus not working on blur

如果驗證失敗,我正在使用jquery將焦點放在模糊的文本框上。 但它在IE中工作但沒有工作FF。 有什么建議么?

$("#inputBoxId").blur(function () {
   if ($(this).val() < 10) 
      $("#inputBoxId").focus();

});

看起來你需要根據這個問題使用setTimeout。 由於您要立即將焦點放在元素上,因此您需要考慮元素首先離開焦點的時間。

$("#inputBoxId").blur(function() {
    if ($(this).val() < 10) {
        setTimeout(function() {
            $("#inputBoxId").focus();
        }, 100);
    }
});

它在jsfiddle上運行的示例 ,在chrome dev通道,firefox和IE8上進行了測試。

val()將返回字符串,而不是數字。 嘗試轉換它應該工作:

var value = parseInt($(this).val(), 10);
if (isNaN(value) || value < 10) 
      $("#inputBoxId").focus();

這也將處理非數值。

//頁面可見性:

    document.addEventListener('visibilitychange', function () {

        if (document.hidden) {
            // stop running expensive task

        } else {
            // page has focus, begin running task

        }
    });

$(this).val().length也會給出長度

$("#inputBoxId").blur(function () {
    if($(this).val().length < 10 ){
          $(this).focus();
     }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM