使用angular8单击选项卡时如何使选项卡聚焦在第一个字段上

How to get the tab focus on the first field when the tab is clicked using angular8

您好,我总共有 6 个选项卡,单击一个选项卡会打开子选项卡。子选项卡打开后,我希望焦点位于每个选项卡的第一个子选项卡上。我尝试通过获取第一个选项卡的 ID 并尝试获取其中的子选项卡 ID。 例如:第一个选项卡是 details_basic_info,它的子选项卡包含 tab-details_basic_info。所以我试图通过添加 'tab-' 和动态获取的主选项卡的 ID 来获取 details_basic_info 并基于此获取 clicked.But 我未能实现。谁能帮我解决这个问题。

提前致谢。

演示: DEMO

如果是我,我可能会做这样的事情(虽然方法处理的重复性较低,但嘿,这只是一个快速的 vanilla js 概念证明,例如与 bootstrap 一起拍打但可以很容易移植到 angular) 所以你不关心第一个可聚焦元素是什么,只要它在可以聚焦的数组中提供。

希望这对您有所帮助,干杯!

const focusableList = 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';

focusFirstFocusableChildPoC = (id) => {

  const contentPane = document.getElementById(id);

  if (contentPane) {
  
      const focusableElements = contentPane.querySelectorAll(focusableList),
            firstFocusable = focusableElements[0];          
            // Typescript will want this to be <HTMLElement>focusableElements[0]; to cast.

      window.setTimeout(function () {
          // Kick this to the back of the line with the 'ol 0 timeout trick.
          firstFocusable ? firstFocusable.focus() : notifyOfFailure();
      }, 0); 
      

  } else {
     notifyOfFailure(); 
  }
}

notifyOfFailure = () => {
  alert('NO FOCUS FOR YOU!');
}
.tab-content {
  padding: 2rem;
}

.tab-content *:focus {
  outline: red 3px dotted;
}

.tab-content *:focus:after {
  content: '*';
  margin-top: 1rem;
  color: red;
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item">
    <a class="nav-link active" data-toggle="tab" href="#panel1" role="tab" onclick="focusFirstFocusableChildPoC('panel1')">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-toggle="tab" href="#panel2" role="tab" onclick="focusFirstFocusableChildPoC('panel2')">Profile</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-toggle="tab" href="#panel3" role="tab" onclick="focusFirstFocusableChildPoC('panel3')">Contact</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-toggle="tab" href="#panel4" role="tab" onclick="focusFirstFocusableChildPoC('panel4')">Contact</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-toggle="tab" href="#panel5" role="tab" onclick="focusFirstFocusableChildPoC('panel5')">Contact</a>
  </li>
</ul>

<div class="tab-content" id="myTabContent">
  <div class="tab-pane show active" id="panel1" role="tabpanel">
    <input placeholder="a text input" type="text">
    <button>A Button</button>
    <input type="checkbox">
    <input type="number">
  </div>
  <div class="tab-pane" id="panel2" role="tabpanel">
    <label><input type="checkbox"> Focus testing</label>
    <input type="text">
    <button>A Button</button>
    <input type="number">
  </div>
  <div class="tab-pane" id="panel3" role="tabpanel">
    <button>A Button</button>
    <input type="text">
    <input type="checkbox">
    <input type="number">
  </div>
  <div class="tab-pane" id="panel4" role="tabpanel">
    <a href="#">Test Anchor Link</a>
    <button>A Button</button>
    <input type="text">
    <input type="checkbox">
    <input type="number">
  </div>
  <div class="tab-pane" id="panel5" role="tabpanel">
    Nothing here to focus bro...
  </div>
</div>