JavaScript:input.focus() 和显示光标
<p><p>如何使用 JavaScript (<strong>no</strong> jQuery) 聚焦文本输入并使 iOS 设备上闪烁的光标/虚拟键盘出现?</p>
<p>这似乎不是您调用时的默认行为:</p>
<pre><code>element.focus();
</code></pre>
<p>使用...的解决方案</p>
<pre><code>element.click();
element.focus();
</code></pre>
<p>...如其他帖子中所建议的那样也不起作用。</p>
<p>谢谢!</p>
<p>编辑:演示:</p>
<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>function focusText(){
document.getElementById('text').focus();
}
function focusCalled(){
document.getElementById('text').value = '';
document.getElementById('text').type = 'password';
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code><input type="text" id="text" value="Password" onfocus="focusCalled();">
<button onclick="focusText();">Click me!</button></code></pre>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>仅在 iOS 中单击“清除”按钮时出现此问题 - 但前提是该值已清除。到目前为止,我发现让光标回到代码中的唯一方法是如下黑客攻击......</p>
<pre><code>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();
}
</code></pre></p>
<p style="font-size: 20px;">关于JavaScript:input.focus() 和显示光标,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/46909790/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/46909790/
</a>
</p>
页:
[1]