我正在做jQuery移動中的移動應用程序。當第一頁加載時,它有一個init函數。我的要求是,當加載第一頁時,光標應該指向文本框。我想:如何在頁面上的文本框中設置光標init
$('#txtdemo').focus();
和
var txtBox=document.getElementById("txtdemo");
txtBox.focus();
但兩者都沒有工作。請讓我知道一個很好的方法來做到這一點。
我正在做jQuery移動中的移動應用程序。當第一頁加載時,它有一個init函數。我的要求是,當加載第一頁時,光標應該指向文本框。我想:如何在頁面上的文本框中設置光標init
$('#txtdemo').focus();
和
var txtBox=document.getElementById("txtdemo");
txtBox.focus();
但兩者都沒有工作。請讓我知道一個很好的方法來做到這一點。
在手機網站,這是不可能使用編程文本框和鍵盤開放重點。這是一種可用性問題。在桌面版網站,你可以這樣做
$('#txtdemo').focus();
$('#txtdemo').select();
$('#txtdemo').trigger('focus');
$('#txtdemo').trigger('click');
謝謝迪文......它適用於我 –
@vinit hasreevijay - 你的歡迎 –
它只能在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>
這裏也沒有指向文本框 –
你使用的是什麼瀏覽器?我的例子已經在Firefox,Chrome和IE9上測試過了,它可以在所有的瀏覽器上運行 – Gajotres
Chrome ..但它不適用於我 –
你有沒有嘗試將.focus調用放在一個setTimeout的時間說,200? – Jeff
@vinithasreevijay,我認爲你不能這樣做,因爲它可能像輸入字段'txtdemo'可能不可用在初始化函數 –
當我給焦點()文本框選擇,但問題是,光標未指向文本框 –