如何将焦点设置在搜索框上? [英] How to set focus on a seach box ?

查看:72
本文介绍了如何将焦点设置在搜索框上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试解决如何将焦点设置在我放在HTML应用的启动屏幕上的搜索框中。

I'm trying to work out how to set the focus on a search box that I have placed on the start up screen of my HTML app.

我希望当应用程序返回浏览屏幕时,或者在执行搜索后刷新屏幕时,它会集中在那里。

I'd like it focus there when ever the app returns to the Browse Screen, or perhaps when the screen is refreshed, after performing the search.

我已尝试将此代码添加到搜索框的帖子呈现代码中: 
$(元素).attr('id','SearchBox');


然后我尝试将此添加到点击事件中Post Render代码中的一个按钮:
$('#SearchBox')。focus();

I've tried adding this code to the post render code of the search box:  $(element).attr('id', 'SearchBox');
Then I've tried adding this to the on-click event of a button within the Post Render code: $('#SearchBox').focus();

但我必须遗漏一些东西,因为没有任何反应。

But it I must be missing something, because nothing happens.

有没有人看过在HTML客户端设置焦点?

Has anyone looked at setting focus in the HTML client?

全部谢谢。

全部谢谢。

推荐答案

嘿John,

如果你'在postRender中重新设置focus(),您可能希望将调用包装在一个setTimeout调用。在postRender和setTimeout将占用该值后,LS和/或JQueryMobile可以完成一些非常重要的DOM工作。 此外,
我知道IE特别是在元素不可见时设置焦点的问题。 

If you're setting focus() in the postRender, you will probably want to wrap the call in a setTimeout call. A non-trivial amount of DOM work may be done by LS and/or JQueryMobile after the postRender and the setTimeout will account for that.  Moreover, I know that IE in particular has issues with setting focus if the element is not visible. 

结果代码如下所示:

    setTimeout(function () {
        if (


('#searchBox')。is(':visible')){
('#searchBox').is(':visible')) {


('#searchBox')。focus() ;
}
},300);
('#searchBox').focus(); } }, 300);

这对我来说是一个快速的样本,但如果不能解决问题请告诉我对你而言。

This worked for me on a quick sample, but please let me know if doesn't solve the problem for you.

谢谢。

Joe


这篇关于如何将焦点设置在搜索框上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆