简体   繁体   English

加载页面时如何将焦点设置到引导选项卡内容中的输入

[英]how to set focus to an input within a Bootstrap tab-content when the page is loaded

on my website done with bootstrap 3.3.6 i have the tabs shown in the image below.在我使用 bootstrap 3.3.6 完成的网站上,我有下图中显示的选项卡。 when the page is loaded, the first tab (containing a login-form) is shown by default.加载页面时,默认显示第一个选项卡(包含登录表单)。 i'd like to set the focus in the input field for the e-mail address (the first one in the image below) when the page is loaded.我想在页面加载时将焦点设置在电子邮件地址的输入字段(下图中的第一个)。 unfortunately, the following jquery-code seems not to be considered:不幸的是,似乎没有考虑以下 jquery 代码:

$(document).ready(function () {
    $("#txtLoginAccountEmail").focus();
});

is there anything else to consider when the focus must be set to an input field which is inside of a tab content?当必须将焦点设置到选项卡内容内的输入字段时,还有什么需要考虑的吗? eg any events from the tab or else...例如选项卡中的任何事件或其他...

在此处输入图像描述

You could use the autofocus attribute in the input tag.您可以在输入标签中使用autofocus属性。 <input type="email" autofocus> should work <input type="email" autofocus>应该可以工作

Simply add active class to first li of tabs只需将活动类添加到选项卡的第一个 li

<ul class="nav nav-tabs">
    <li class="active"><a href="#">Login</a></li>
    <li><a href="#">Registration</a></li>
    <li><a href="#">Guest Account</a></li>
</ul>

Or by jquery或者通过 jquery

$(".nav-tabs li:first-child").addclass('active');

This is my method if autofocus doesn't work如果autofocus不起作用,这是我的方法

  
$(document).on("shown.bs.tab", "button[data-bs-toggle='tab']", function(event) { 
  // event.target // newly activated tab
  // event.relatedTarget // previous active tab
  var activeTab = $('.tab-content .active');
  var focucedInp = $(activeTab).find('input[autofocus="true"]')
  setTimeout(function() { $(focucedInp).focus() }, 200);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM