Mib*_*uko 11 html javascript safari ios vue.js
我有一个无法解决的问题。
IOS上的input.focus()上不显示键盘
searchMobileToggle.addEventListener('click', function() {
setTimeout(function(){
searchField.focus();
}, 300);
});
Run Code Online (Sandbox Code Playgroud)
我一直在寻找没有结果的解决方案,我知道这是一个经常未解决的问题,但我看到了NIKE ( https://m.nike.com/fr/fr_fr/ ) 和FOODPRING ( https://www.foodspring .fr/ ) 在移动设备上进行。
所以我想知道他们是怎么做的?
n8j*_*ams 12
其他答案都不适合我。我最终查看了 Nike javascript 代码,这就是我想出的可重用函数:
function focusAndOpenKeyboard(el, timeout) {
if(!timeout) {
timeout = 100;
}
if(el) {
// Align temp input element approximately where the input element is
// so the cursor doesn't jump around
var __tempEl__ = document.createElement('input');
__tempEl__.style.position = 'absolute';
__tempEl__.style.top = (el.offsetTop + 7) + 'px';
__tempEl__.style.left = el.offsetLeft + 'px';
__tempEl__.style.height = 0;
__tempEl__.style.opacity = 0;
// Put this temp element as a child of the page <body> and focus on it
document.body.appendChild(__tempEl__);
__tempEl__.focus();
// The keyboard is open. Now do a delayed focus on the target element
setTimeout(function() {
el.focus();
el.click();
// Remove the temp element
document.body.removeChild(__tempEl__);
}, timeout);
}
}
// Usage example
var myElement = document.getElementById('my-element');
var modalFadeInDuration = 300;
focusAndOpenKeyboard(myElement, modalFadeInDuration); // or without the second argument
Run Code Online (Sandbox Code Playgroud)
请注意,这绝对是一个笨拙的解决方案,但 Apple 已经很久没有解决这个问题的事实证明了这一点。
我找到了一个解决方案,click() 没有用,但我想通了。
searchMobileToggle.addEventListener('click', function() {
if(mobileSearchblock.classList.contains('active')) {
searchField.setAttribute('autofocus', 'autofocus');
searchField.focus();
}
else {
searchField.removeAttribute('autofocus');
}
});
Run Code Online (Sandbox Code Playgroud)
我正在使用 vue.js,它autofocus在加载组件时删除输入属性。所以我点击了它,但还有另一个问题,自动对焦只工作一次,但与 focus() 结合,它现在一直工作:)
感谢您的帮助 !