【问题标题】:How to get the tab focus on the first field when the tab is clicked using angular8使用angular8单击选项卡时如何将选项卡焦点放在第一个字段上
【发布时间】:2020-03-13 18:45:11
【问题描述】:

您好,我总共有 6 个标签,单击一个标签会打开子标签。一旦子选项卡打开,我希望焦点放在每个选项卡的第一个子项上。我尝试获取第一个选项卡的 ID 并尝试获取其中的子选项卡 ID。 例如:第一个标签是details_basic_info,它的子标签包含tab-details_basic_info。所以我试图通过添加'tab-'来获取details_basic_info,并在此基础上添加'tab-'和点击它的动态获取的主选项卡的ID。但我未能实现。谁能帮帮我,如何做到这一点。

提前致谢。

演示: DEMO

【问题讨论】:

    标签: javascript angular tabindex


    【解决方案1】:

    如果是我,我可能会做这样的事情(虽然方法处理的重复性要少一些,但是嘿,这只是一个快速的 vanilla js 概念证明,例如与引导程序一起拍打,但可以很容易地移植到角度)因此,您不必关心第一个可聚焦元素是什么,只要它在可聚焦的数组中提供即可。

    希望这会有所帮助,干杯!

    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>

    【讨论】:

    • 我通常不喜欢在这里为别人工作,但现在是星期五,所以这应该能让你大部分时间都走,但如果是我,我会更简洁地实现它,干杯! Forked DEMO with changes
    • 非常感谢,但如果您感觉不好,我们深表歉意。我的意图并非如此。但是非常感谢它真的节省了我的周末,我现在可以去做其他工作了
    • 很抱歉,我还没有在 snipnet 中看到您附上的演示以及您的答案。我不知道这是一个互联网问题。现在我可以访问您的 sn-p 代码以及分叉的演示。谢谢但抱歉
    • 嗨,克里斯,我在我的代码中尝试了同样的方法,但是这里的标签焦点是第二次点击,可能是什么问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多