【问题标题】:Custom HTML Checkbox symbols with keyboard navigation support?具有键盘导航支持的自定义 HTML 复选框符号?
【发布时间】:2014-05-26 23:51:33
【问题描述】:

我可以替换传统的 HTML 复选框符号“未选中”、“选中”和 带有自定义符号的“不确定”:

<link rel="stylesheet" 
  href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css">
<style type="text/css">
  input[type="checkbox"] {
    display: none;
  }
  input[type="checkbox"]+label:before {
    font-family: FontAwesome;
    content: "\f096"; /* fa-square-o */
  }
  input[type="checkbox"]:checked + label:before {
    content: "\f046"; /* fa-check-square-o */
  }
  input[type="checkbox"]:indeterminate + label:before {
    content: "\f0c8"; /* fa-square */
    color: grey;
  }
</style>
...
<input type="checkbox" id="cb1" />
<label for="cb1"> label text</label>

http://plnkr.co/SPvN1xEkQqcFcYbxWur2

除了键盘导航和控制之外,这非常有用。使用传统的复选框,您可以 [TAB] 通过输入元素和链接,并使用 [SPACE] 和 [ENTER] 访问它们。自定义符号使用 display:none,这似乎禁用了对这些元素的键盘控制。

如何为这些自定义复选框重新启用键盘控制?

【问题讨论】:

    标签: html css checkbox keyboard accessibility


    【解决方案1】:

    与其将复选框设置为display: none,不如将​​它们隐藏在后面。您仍然可以通过 Tab 键聚焦并保留其他键盘事件。

    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css">
    <style type="text/css">
      input[type="checkbox"] {
        position:absolute;
        margin: 2px 0 0;
        z-index:0;
      }
    
      input[type="checkbox"]+label:before {
        position: relative;
        z-index: 5;
        background: white;
        font-family: FontAwesome;
        content: "\f096";
      }
    
      input[type="checkbox"]:checked + label:before {
        position: relative;
        z-index: 5;
        background: white;
        content: "\f046";
      }
    
      input[type="checkbox"]:indeterminate + label:before {
        position: relative;
        z-index: 5;
        background: white;
        content: "\f0c8";
        color: grey;
      }
    </style>
    ...
    <input type="checkbox" id="cb1" />
    <label for="cb1"> label text</label>
    

    现场示例:http://plnkr.co/edit/6lzcJIdMWFRzVNQBxRPu?p=preview

    【讨论】:

    • 出色而快速的答案。我什至没有想到将原始复选框隐藏在新符号后面。非常感谢。
    • @Yurim 没问题,很高兴我能帮上忙。
    • before: and after: ie8 后面缺少浏览器支持。这中和了隐藏而不是“显示:无”的好方法;本机复选框。更好地使用边距来调整元素。
    猜你喜欢
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2016-05-13
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多