JavaScript:input.focus() 并显示 Cursor

标签 javascript ios input cursor

如何使用 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="Password" onfocus="focusCalled();">
<button onclick="focusText();">Click me!</button>

最佳答案

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

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() 并显示 Cursor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46909790/

相关文章:

python - PyQt5 和 Python 中的用户输入验证

c - 从文件 C 中的单行读取多个变量类型

javascript - "target element is not a descendant of root"是什么意思?

javascript - 在 Javascript 中比较两个无序对象数组的自定义实现

javascript - 将对象推送到数组并使用另一个对象作为属性

javascript - 在 ionic 2 中不同祈祷时间播放声音

ios - 替代动画(forKey :) (now deprecated)?

javascript - m3u8 + 404 + ios Safari = 烦人的弹出窗口 : "The requested URL was not found on this server"

ios - 无法在 IOS 模拟器上启动应用程序 - WebDriver 代理错误

C if else 循环不起作用