如何将Focus添加到表行<tr>?

Sou*_*ter 2 css focus html-table angularjs

我有一个用AngularJS生成的表.
这是我的HTML:

<table class="my_table">
    <thead>
         <tr>
            <th>Name</th>
            <th>Address</th>
            <th>Celphone</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="contact in contacts">
            <td>{{contact.Name}}</td>
            <td>{{contact.Address}}</td>
            <td>{{contact.Celphone}}</td>
        </tr>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

我希望每行在"悬停"时改变颜色,在"点击"时改变不同颜色,所以我尝试用CSS:

<style>
.my_table tbody tr:hover {
   background-color: #7fccee; /*--- this is a blue color when hover ---*/
}
.my_table tbody tr:focus {
   background-color: #f3f3f3; /*--- this is a gray color when clicked---*/
}
</style>
Run Code Online (Sandbox Code Playgroud)

悬停工作完美,但焦点不起作用!(奇怪的是,在浏览器控制台中,如果我选择行和" 强制元素状态:焦点 ",那么它将焦点应用于行)

我也尝试过使用带有jquery的SCRIPT:

$(document).ready(function() {
   $('.my_table tbody tr').click(function() {
      $(this).addClass('active'); //I'm adding the color gray with css to '.active'
   });
});
Run Code Online (Sandbox Code Playgroud)

但这也行不通,我做错了什么?

Sti*_*ers 7

:focus伪类只适用于表单元素,例如<input>,<button>等等.你可以把它添加到非表单元素一样<tr>加入tabindex,例如:

<tr tabindex="0">
Run Code Online (Sandbox Code Playgroud)

然后照常应用CSS.

.my_table tbody tr:hover {
  background-color: blue;
}

.my_table tbody tr:focus {
  background-color: red;
  outline: 0; /*remove outline*/
}
Run Code Online (Sandbox Code Playgroud)
<table class="my_table" border>
  <thead>
    <tr>
      <th>Name</th>
      <th>Address</th>
      <th>Celphone</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="contact in contacts" tabindex="0">
      <td>{{contact.Name}}</td>
      <td>{{contact.Address}}</td>
      <td>{{contact.Celphone}}</td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)