admin管理员组文章数量:1429486
I have a list of input
s under a list of div
s respectively. I have a button that when clicked it will switch one input from from not display to display. This is something like:
if (switched) {
document.getElementById("div-xxx").style.display = "block";
}
However, is there a way I could make the input
inside the displayed div
being auto focused after this switch? I tried something like
document.getElementById('input-xxx').autofocus = true;
after the display
code, but there is no autofocus at all.
I have a list of input
s under a list of div
s respectively. I have a button that when clicked it will switch one input from from not display to display. This is something like:
if (switched) {
document.getElementById("div-xxx").style.display = "block";
}
However, is there a way I could make the input
inside the displayed div
being auto focused after this switch? I tried something like
document.getElementById('input-xxx').autofocus = true;
after the display
code, but there is no autofocus at all.
3 Answers
Reset to default 3document.getElementById('input-xxx').focus()
will change the focus to the selected element.
document.getElementById('input-xxx').setAttribute('autofocus', true)
will assign the autofocus
attribute to the html element
object.focus(); will help
if (switched) {
document.getElementById('div-xxx').style.display = "block";
document.getElementById('input-xxx').focus();
}
The only thing that worked for me was
if (switched) {
document.getElementById('div-xxx').style.display = "block";
document.getElementById('old-input').setAttribute('autofocus', false);
document.getElementById('input-xxx').setAttribute('autofocus', true);
document.getElementById('input-xxx').focus();
}
本文标签: javascriptHow to autofocus after changing display from none to blockStack Overflow
版权声明:本文标题:javascript - How to auto-focus after changing display from none to block? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745504598a2661187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论