如何使用JavaScript将焦点设置在隐藏的文本框字段上? [英] How to set the focus on a hidden textbox field using JavaScript?
问题描述
如何将焦点设置在隐藏的文本框元素上?
How can I set the focus on a hidden textbox element?
我试过这个:
document.getElementById("textControl_fd_component_JSON_TASK_NStext64").focus();
但是,这在这里不起作用。作为 alert(document.activeElement.id);
在这种情况下返回 null
。
But, this does not work here. As alert(document.activeElement.id);
returns null
in this case.
如果我将此字段显示,则上述脚本可以正常工作。任何想法,我错了吗?
If I make this field visible, the above script works fine. Any ideas where I am getting it wrong?
推荐答案
如果你真的需要这样做,请使框透明,而不是隐藏:
If you really need to do this, make the box transparent, not hidden:
opacity:0;
filter:alpha(opacity=0);
或者,如果您想确保用户不小心点击它,只需输入输入在div内部
Alternatively, if you want to ensure that the user doesn't accidentally click it, just place the input inside a div with
width: 0;
overflow: hidden;
然而,肯定有更好的方法可以做你想做的事,也许使用 keydown
/ keypress
事件。
However, there is most certainly a better way to do what you want, maybe using keydown
/keypress
events.
这篇关于如何使用JavaScript将焦点设置在隐藏的文本框字段上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!