菜鸟教程小白 发表于 2022-12-11 19:04:41

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(&#39;text&#39;).focus();

}

function focusCalled(){

document.getElementById(&#39;text&#39;).value = &#39;&#39;;
document.getElementById(&#39;text&#39;).type = &#39;password&#39;;

}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;input type=&#34;text&#34; id=&#34;text&#34; value=&#34;Password&#34; onfocus=&#34;focusCalled();&#34;&gt;
&lt;button onclick=&#34;focusText();&#34;&gt;Click me!&lt;/button&gt;</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 &gt; 0) oInput.value = &#39;&#39;;
    else
    {
      oInput.value = &#39; &#39;;
      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]
查看完整版本: JavaScript:input.focus() 和显示光标