【问题标题】:Setting focus on table row elements将焦点放在表格行元素上
【发布时间】:2017-06-22 05:54:51
【问题描述】:

我有一个项目列表,我希望能够使用键盘向上和向下按钮滚动浏览这些项目。我目前能够获得类似的预期效果,即使用 tabindex 属性向下移动列表。我还使用 ng repeat 来生成元素的实例。关于如何通过上下键实现聚焦和上下滚动列表的提示或线索?

下面的示例代码。

table ng-if="!toolbarCtrl.loadingContacts" class="table dataTable hover" id="table3">
                <tbody role="alert" aria-live="polite" aria-relevant="all">
                    <tr class="pointer" tabindex="0" ng-repeat="contact in toolbarCtrl.contactEntities | filter:toolbarCtrl.toolbarSearchStringAfterDelay | orderBy: toolbarCtrl.tab == toolbarCtrl.tabs.RECENT ? '-dateSeen' : (toolbarCtrl.tab == toolbarCtrl.tabs.ALL ? '+data.fileAs' : '')"
                        ng-class="" ng-if="toolbarCtrl.toolbarSearchStringAfterDelay" ng-click="toolbarCtrl.selectContact(contact)"> 
                        <td rel="popover_dark" ng-class="contact.loading ? 'contactDisabled' : '' " data-container="body" data-toggle="popover" data-placement="right" data-content="" data-original-title="" title="" class="sorting_1 has_popover">
                        </td>
                    </tr>
                </tbody>
            </table>

我将你的 checkKey 函数变成了这个

 controller.checkKey = function (event) {
        $("tr[tabindex=0]").focus();
        var event = window.event;
        if (event.keyCode == 40) { //down
            var idx = $("tr:focus").attr("tabindex");
            idx++;
            if (idx > 6) {
                idx = 0;
            }
            $("tr[tabindex=" + idx + "]").focus();
        }
        if (event.keyCode == 38) { //up
            var idx = $("tr:focus").attr("tabindex");
            idx--;
            if (idx < 0) {
                idx = 6;
            }
            $("tr[tabindex=" + idx + "]").focus();
        }
    }

我很接近,但它直接跳到最后一个元素。这看起来对吗?

【问题讨论】:

  • 我明白为什么你也得到了所有的 tabindex="0"...你没有为 tabindex 值使用变量,你每次都将它分配给 0。跨度>

标签: javascript jquery html angularjs


【解决方案1】:

这里有一个处理向上/向下箭头的 keydown 事件以导航表格行的小技巧 (&lt;tr&gt;)。您可能需要根据生成的数据调整上下 tabindex 处理,但这取决于您。 GL!

(jsfiddle:https://jsfiddle.net/qkna8jgu/2/)

示例 HTML:

<table>
  <tr tabindex="0">
    <td>first row</td>
  </tr>
  <tr tabindex="1">
    <td>second row</td>
  </tr>
  <tr tabindex="2">
    <td>third row</td>
  </tr>
  <tr tabindex="3">
    <td>fourth row</td>
  </tr>
  <tr tabindex="4">
    <td>fifth row</td>
  </tr>
  <tr tabindex="5">
    <td>sixth row</td>
  </tr>
  <tr tabindex="6">
    <td>seventh row</td>
  </tr>
</table>

jQuery:

$(document).ready(function() {
    $("tr[tabindex=0]").focus();    
    document.onkeydown = checkKey;
});

function checkKey(e) {
    var event = window.event ? window.event : e;
    if(event.keyCode == 40){ //down
      var idx = $("tr:focus").attr("tabindex");
      idx++;
      if(idx > 6){
        idx = 0;
      }
      $("tr[tabindex="+idx+"]").focus();
    }
    if(event.keyCode == 38){ //up
      var idx = $("tr:focus").attr("tabindex");
      idx--;
      if(idx < 0){
        idx = 6;
      }
      $("tr[tabindex="+idx+"]").focus();      
    }
}

【讨论】:

  • 我已经接近正确了,但是不同的 是用 angular 生成的,因此它们都有一个 tabindex=0。我知道这应该让它们按视觉逻辑顺序导航。目前,向下和向上键聚焦在列表底部的 实例。
  • 我明白了。害羞的角度来阻止它,你可以使用 jQuery $("tr[tabindex=0]").each 来修复你的 &lt;tr&gt; 的 tabindex 值,如果这是你的一个选项。
  • 我唯一的困惑是$("tr[tabindex=0]").each(function(idx){ $(this).attr("tabindex",idx); }); 这是否将数组中的每个项目的tabindex 值设置为= idx?
  • 没错。在我看来,您应该在构建表格的角度代码中处理 tabindex 值,而不是为每一行分配零,但是如果这不灵活,我今天提供的代码将使用 jquery 重新索引 tabindex 值循环(each)。该代码获取所有&lt;tr tabindex="0"&gt; 元素,循环遍历并将tabindex 属性值重新分配给循环中的索引值。这会产生正确递增的 tabindex 值,因此剩余的向上/向下导航可以正常工作。
  • 我的理解是您的方法将 tabindex 重新编号为 i++ 增量,因此我不需要在开始时将 tab index 属性设置为 0。
【解决方案2】:

这是我使用的方法。它将 jquery 的焦点放在使用名称和角度索引创建的自定义 id 上。 preventDefault 仅仅是因为向上和向下键也在上下移动我的页面,这消除了这一点。

controller.checkKey = function (event, index) {
        event.preventDefault();
        if (event.keyCode === 40) {
            $('#item-' + (++index)).focus();
        }
        if (event.keyCode === 38) {
            $('#item-' + (--index)).focus();
        }
    };

这种方法适用于角度范式,上面的 jquery 示例也很棒。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签