简体   繁体   English

延迟()不适用于焦点()

[英]Delay() isn't working on focus()

 $(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> 

Problem I have with the above code is that the delay() is not working on the focus() . 上面的代码存在的问题是delay()focus()上不起作用。

The documentation states that delay only works for animations (unless you set up a queue) , you probably just want a setTimeout instead 文档指出, delay仅适用于动画(除非您设置队列) ,您可能只想使用setTimeout即可

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

For completeness, using delay with a queue 为了完整delay ,请在队列中使用delay

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

You can try an alternative to achieve this, 您可以尝试其他方法来实现这一目标,

 $(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> 

The .delay() method is best for delaying between queued jQuery effects. .delay()方法最适合在排队的jQuery效果之间进行延迟。 Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases. 因为它是有限的(例如,它没有提供消除延迟的方法),所以.delay()不能替代JavaScript的本机setTimeout函数,后者可能更适合某些用例。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM