Javascript 如何在弹出窗口中的提交按钮上设置默认焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6544438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set default focus on the submit button in a popup window
提问by srinivas
please help me someone,,,,
请帮我一个人,,,,
i write some code to get the popup window using the following code.
我编写了一些代码来使用以下代码获取弹出窗口。
<div id="EMAIL_CONFIRMATION_PENDING" class="popupContact" style="z-index:10003;">
<div class="popup_textarea">
<h3><h:outputLabel value="#{labelMsgs.emailPendingLabel}"/></h3>
<div class="VerifyEmailText">
<h:outputLabel value="#{labelMsgs.clickToSendEmail}"/>
<span class="FontItalic"><h:outputLabel value="#{headerBean.emailAddress}."/></span>
<br /><br />
</div>
<div class="SuspendedBoxMsg">
<h:outputLabel value="#{labelMsgs.accountSuspend}"/><br />
<h3><h:outputLabel value="#{sessionScope.DASHBOARD_EMAIL_EXP_TIME}"/></h3>
<h:outputLabel value="#{labelMsgs.unlessYouVerify}"/>
<br />
</div>
<div>
<span class="FontWeight">
<h:outputLabel value="#{labelMsgs.spamMailFolder}"/>
</span>
<a4j:commandLink id="resendLink" styleClass="violet_color_link" value="#{labelMsgs.regSuccSendMail}" onclick="javascript:resendLink(); this.onclick=null;" action="#{headerBean.resendEmail}" />
</div>
<div class="OkButton">
<div class="button_input">
<a4j:commandButton id="emailConfm" styleClass="image_button" value="#{labelMsgs.okButton}" action="#{accntDashboardBean.popupRefresh}"
reRender="frmAccountDashboardMenu:POPUP_PANEL" oncomplete="disablePopup1('EMAIL_CONFIRMATION_PENDING', 'backgroundPopup');popupCall('#{sessionScope.toShowPopupOf}');"/>
</div>
</div>
</div>
</div>
in this am using the id of the div to populate the popup,like this i have write some other popup code with different ids. for this i write the code in window.onload function to get the popup, so, i need to set the default focus on the submit button which i mentioned in the above code.
在此我使用 div 的 id 来填充弹出窗口,像这样我编写了一些其他具有不同 id 的弹出代码。为此,我在 window.onload 函数中编写代码以获取弹出窗口,因此,我需要将默认焦点设置在我在上面代码中提到的提交按钮上。
回答by daniel.herken
回答by Vishal Vijayan
you can use the following to set focus into the button
您可以使用以下内容将焦点设置为按钮
document.getElementById('emailConfm').focus();
But this may not work, to fix this I have done something like this
但这可能行不通,为了解决这个问题,我做了这样的事情
setTimeout(function() { document.getElementById('emailConfm').focus(); },100);
回答by Vivek Mishra
You can use HTML form tag inside your div to set focus on Submit button. Use your input tags inside form tag, so whenever you are entering User Name or Password Submit button is always focussed on press of Enter button.
您可以在 div 中使用 HTML 表单标签将焦点设置在提交按钮上。在表单标签中使用您的输入标签,因此每当您输入用户名或密码时,提交按钮始终专注于按下 Enter 按钮。
<form>
<input type="text"/>
<input type="password"/>
<input type="submit"/>
</form>