我想在 setTimeout 函数内触发对移动设备输入的焦点。这工作正常:
$('input').on('touchstart', function(){
$(this).focus();
});
$('input').trigger('touchstart');
但是,这不起作用:
setTimeout(function(){
$('input').on('touchstart', function(){
$(this).focus();
});
$('input').trigger('touchstart');
},200);
占位符消失,就好像输入已聚焦一样,但键盘和光标不显示。我不知道为什么。有什么办法可以做到这一点吗?
将
超时与设置侦听器分开:
// It's ok to listen
$('input').on('touchstart', function(){
$(this).focus();
});
// Trigger only when you want it
setTimeout(function(){
$('input').trigger('touchstart');
},200);
我不知道
为什么你需要超时,但这似乎有效。
$('input').on('touchstart', function() {
$(this).focus();
});
setTimeout(function() {
$('input').trigger('touchstart');
}, 200);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" placeholder="foo"></input>