为什么不这个JavaScript焦点()工作? [英] Why doesn't this javascript focus() work?

查看:87
本文介绍了为什么不这个JavaScript焦点()工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个index.html,它有一个巨大的形式。
表单由此javascript提交:

  byId(p)。value = page; 
byId(nav_container)。focus();
document.forms [nav_form_main]。submit();

焦点部分在这里不起作用...



我希望浏览器专注于页面的特定部分(几乎在顶部)。



要做什么的提示?



PS:我已经尝试在submit()后重点关注,同样的问题也存在。

谢谢

解决方案

确保您要关注的元素具有属性 tabindex = - 1 ,否则该元素不可聚焦。



例如

 < ; div id =myfocusabledivtabindex = -  1>< / div> 

当你为一个元素设置tabindex = -1时,它允许它通过javascript获得焦点() 。如果您希望它通过元素标签来获得焦点,那么您应该将tabindex属性设置为0.


I have an index.html which has a huge form in it. The form is submitted by this javascript:

byId("p").value = page;
byId("nav_container").focus();
document.forms["nav_form_main"].submit();

The focus part doesn't work here...

I want the browser to focus on a specific part of the page (almost at top).

Any tips of what to do?

PS: I have tried putting the focus after the submit(), same issue there.

Thanks

解决方案

make sure the element you want to focus has an attribute tabindex="-1", otherwise that element is not focusable.

For example

<div id="myfocusablediv" tabindex="-1"></div>

When you set the tabindex=-1 for an element it allows it to get focus() through javascript. If instead you want it to get focus through tabbing through elements, then you would set the tabindex attribute to 0.

这篇关于为什么不这个JavaScript焦点()工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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