2017-04-04 28 views
1

延時()是不工作的焦點()

$(document).ready(function() { 
 
    $(".cl").on("click", function() { 
 
    $(".inpt").delay(5000).focus(); 
 
    }); 
 
});
.cl { 
 
    background: #009; 
 
    width: 300px; 
 
    height: 100px; 
 
    line-height: 100px; 
 
    color: #fff; 
 
    text-align: center; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" class="inpt"> 
 
<div class="cl">click me</div>

問題我有與上面的代碼是,delay()不工作在focus()

回答

4

的文檔指出delay僅適用於動畫(除非您設置了一個隊列),你可能只是想要一個setTimeout的,而不是

$(document).ready(function() { 
    $(".cl").on("click", function() { 
     setTimeout(function() { 
      $(".inpt").focus(); 
     }, 5000); 
    }); 
}); 

爲了完整起見,使用delay與隊列

$('.inpt').delay(5000).queue(function (next) { 
    $(this).focus(); 
    next(); 
}); 
+0

謝謝,adeneo – sorvz

2

您可以嘗試一種替代方案來實現此目的,

$(document).ready(function() { 
 
    $(".cl").on("click", function() { 
 
    setTimeout(function(){ 
 
    $(".inpt").focus(); 
 
    },5000); 
 
    }); 
 
});
.cl { 
 
    background:#009; 
 
    width:300px; 
 
    height:100px; 
 
    line-height:100px; 
 
    color:#fff; 
 
    text-align:center; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" class="inpt"> 
 
<div class="cl">click me</div>

的.delay()方法是最適合排隊jQuery效果之間延遲。因爲它是有限的,所以它不提供取消延遲的方法.delay()並不是JavaScript的本地函數setTimeout的替代品,這對於某些用例可能更合適。

我希望這會有所幫助。

+0

謝謝,它幫助:) – sorvz