10

我認爲焦點事件不適用於JQuery移動:這是我的代碼?你怎麼想它,謝謝你。
(當我刪除調用庫jQuery Mobile的,它的工作原理)。焦點()與JQUERY MOBILE的問題?

<!DOCTYPE html> 
<html> 
    <head> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" /> 
     <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script> 
     <script src="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.js"></script> 
    </head> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#acceuil').live('pagecreate', function(event) { 
       $('#declencher').click(function() { 
        $('#cache').focus(); 
       }); 
       $('#declencher').trigger('click'); 
      }); 
     }); 
    </script> 
    <body> 
     <div data-role="page" id ="acceuil" > 
      <div data-role="header" data-theme="a" ><h3>aaa</h3></div> 
      <div data-role="content"> 
       <input id="cache" type="input">  
       <input type="button" id="declencher" value="declencher"> 
      </div><!-- content--> 
      <div data-role="footer" data-theme="a" data-position="fixed"><h3> Footer </h3></div> 
     </div> 
    </body> 

</html> 
+0

更清潔的問題通常得到更快的回答.... –

+0

你試過去除'$(document).ready()'?按照doco的建議,你應該將事件綁定到'pagecreate'而不是'document.ready'。 –

+0

謝謝你的寫作,我想我的問題是明確的人!我已經刪除了$(document).ready()但它沒有工作,我認爲focus()不支持jquery mobile –

回答

9

pagecreate事件觸發JQM做一些修改DOM之前,所以我想重點丟失了,。

嘗試切換到pageshow,尤其是因爲您希望每次用戶進入頁面時都能獲得焦點。

如果仍然無法正常工作(有這樣的情況)包裹在超時觸發焦點代碼(是的,這是一個黑客:))

setTimeout(function(){ 
$('#cache').focus(); 
},0); 

這是一個黑客,但它不依賴於等待時間間隔。 setTimeout()在給定的時間之後,將函數添加到呈現線程的隊列(這是在瀏覽器中運行JavaScript和頁面呈現的東西)。所以在這種情況下,該功能會立即添加,因此它在當前的JavaScript代碼完成後運行。所以這是在事件處理程序結束後立即運行一些代碼的一種方法。所以這不像人們想象的那樣黑客。我把它稱爲黑客攻擊,因爲它使用了關於瀏覽器如何工作的知識,並且掩蓋了代碼執行的流程。

我推薦閱讀關於javascript執行和頁面繪圖如何由單個線程中的相同隊列處理的內容。任何人使用超過20行的JavaScript。

我確信只有一個更好的解決方案 - 修復它在jQuery Mobile框架本身。

+1

它與setTimeout一起工作...,謝謝你 –

+0

我一直在努力尋找一個更優雅的解決方案來解決這個問題。這可以工作,但在不同的硬件上這個黑客可能會不穩定地工作。我已經嘗試過在事件中構建的jquery mobile的全部範圍。沒有這種攻擊就可以實現最好的效果,就是讓焦點上的「暈」(發光效果)成爲焦點。光標不顯示。任何人有任何其他想法? – mfalto

+0

這個黑客不會依賴硬件。請參閱編輯。 – naugtur

2

如果您使用HTML5 ,則使用autofocus屬性。

<body> 
    <div data-role="page" id ="acceuil" > 
     <div data-role="header" data-theme="a" ><h3>aaa</h3></div> 
     <div data-role="content"> 
      <input id="cache" type="input" autofocus>  
      <input type="button" id="declencher" value="declencher"> 
     </div><!-- content--> 
     <div data-role="footer" data-theme="a" data-position="fixed"><h3> Footer  </h3></div> 
    </div> 
</body> 

請參閱Autofocus attribute of HTML5

1

試試這個,它適用於我的移動Safari瀏覽器,Chrome和也的桌面瀏覽器

// div is some selected element 
      var f = function(event) { 
       $timeout(function() { // angular way, setTimeout is OK 
        input[0].focus(); 
        event.preventDefault(); 
       }) 
      }; 

      var mobile = false; 
      div.on('click', function(event) { 
       if(mobile) return; 
       f(event); 
      }); 

      div.on('touchstart', function(event) { 
       mobile = true; 
       f(event); 
      }); 

      div.on('touchend', function(event) { 
       event.preventDefault(); 
       event.stopPropagation(); 
      }); 
-1

這只是另一種選擇,而不是確切的解決方案,如果上述解決方案不起作用,則這可以用於該目的。

在點擊銷售鏈接時,焦點將轉向銷售免責聲明,在這種情況下,偏差量將按照您的要求提及。

<div class="sales"> 
    <a href="#sales-disclaimer" onclick="setFocusToSales()"> 
     Sales 
    </a> 
</div> 

<div id='sales-disclaimer'> 
    some text here........ 
</div> 

/JavaScript函數/

function setFocusToSales() 
{ 
    $('html, body').animate({ 
     scrollTop: $('#sales-disclaimer').offset().top 
    }, 1000); 
} 
-1

解決

$(document).on("pageshow", "#casino", function(event) { 
    var txtBox=document.getElementById("SearchUsuario"); 
    txtBox.tabindex=0; //this do the trick 
    txtBox.value=""; 
    txtBox.focus(); 
}); 
0

在您的UIWebView,設置keyboardDisplayRequiresUserAction爲NO。

如果您在配置中使用Cordova或Phonegap。xml添加以下內容:

<preference name="KeyboardDisplayRequiresUserAction" value="false"/>