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"/>