繁体   English   中英

如何在第init页的文本框中设置光标

[英]How to set the cursor in text box on page init

我在jquery mobile中做移动应用程序。 当第一页加载时,它有一个init函数。 我的要求是,当加载第一页时,光标应指向文本框。 我试过了:

  $('#txtdemo').focus();

   var txtBox=document.getElementById("txtdemo");
   txtBox.focus();

但两者都不起作用。 请让我知道这样做的好方法。

在移动网站中,无法使用编程将焦点放在文本框中并打开键盘。 这是一种可用性问题。 在桌面网站中,您可以执行此操作

$('#txtdemo').focus();

$('#txtdemo').select();

$('#txtdemo').trigger('focus');

$('#txtdemo').trigger('click');

它只能在pageshow事件期间完成。 它必须是页面pageshow因为页面仅在该点完全形成。 如果你不知道什么pageshow是看看我的文章关于jQuery Mobile的页面事件。 此外,这只适用于桌面浏览器,它不适用于移动浏览器。 在移动浏览器的情况下,输入字段将处于焦点,但这不会触发键盘显示。

$(document).on('pageshow', '#index', function(){ 
    $('#some-input').focus();
}); 

工作范例:

<!DOCTYPE html>
<html>http://jsfiddle.net/Gajotres/PMrDn/#update
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>  
        <script>
            $(document).on('pageshow', '#index', function(){ 
                $('#some-input').focus();
            }); 
        </script>       
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="b" data-role="header">
                <h1>Index page</h1>
            </div>

            <div data-role="content">
                <input type="text" value="" id="some-input"/>  
            </div>
        </div>    
    </body>
</html>   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM