OGeek|极客世界-中国程序员成长平台

标题: JavaScript:input.focus() 和显示光标 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:04
标题: JavaScript:input.focus() 和显示光标

如何使用 JavaScript (no jQuery) 聚焦文本输入并使 iOS 设备上闪烁的光标/虚拟键盘出现?

这似乎不是您调用时的默认行为:

element.focus();

使用...的解决方案

element.click();
element.focus();

...如其他帖子中所建议的那样也不起作用。

谢谢!

编辑:演示:

function focusText(){

  document.getElementById('text').focus();

}

function focusCalled(){

  document.getElementById('text').value = '';
  document.getElementById('text').type = 'password';

}
<input type="text" id="text" value="assword" onfocus="focusCalled();">
<button onclick="focusText();">Click me!</button>


Best Answer-推荐答案


仅在 iOS 中单击“清除”按钮时出现此问题 - 但前提是该值已清除。到目前为止,我发现让光标回到代码中的唯一方法是如下黑客攻击......

function ClearInput(sInput)
{
    var oInput = document.getElementById(sInput);
    if (oInput.value.length > 0) oInput.value = '';
    else
    {
        oInput.value = ' ';
        setTimeout(function() { ClearInput(sInput); },
            gkiMinTOutTms); // gkiMinTOutTms=20
    }
    oInput.focus();
}

关于JavaScript:input.focus() 和显示光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46909790/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4