javascript - CSS 样式的单选按钮不响应键盘

标签 javascript html css

我正在使用 CSS 来设置我的单选按钮的样式,就像 iOS 分段按钮一样。他们不响应键盘输入。我错过了什么?

这是一个示例的 HTML:

  <nav class="segmented-button">
      <input type="radio" name="seg-1" value="Organisation" id="seg-Organisation" checked>
      <label for="seg-Organisation" class="first">Organisation</label>
      <input type="radio" name="seg-1" value="Users" id="seg-Users">
      <label for="seg-Users">Users</label>
      <input type="radio" name="seg-1" value="Units" id="seg-Units" disabled>
      <label for="seg-Units">Units</label>
      <input type="radio" name="seg-1" value="Tags" id="seg-Tags">
      <label for="seg-Tags" class="last">Tags</label>
    </nav>

这是CSS

.segmented-button {
  padding: 12px;
}
.segmented-button input[type="radio"] {
  width: 0px;
  height: 0px;
  display: none;
}
.segmented-button label {
  display: -moz-inline-box;
  -moz-box-orient: vertical;
  display: inline-block;
  vertical-align: middle;
  *vertical-align: auto;
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
  -o-border-radius: 4px;
  -ms-border-radius: 4px;
  -khtml-border-radius: 4px;
  border-radius: 4px;
  text-shadow: white;
  background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #ffffff), color-stop(100%, #e4e4e4));
  background: -webkit-linear-gradient(#ffffff, #e4e4e4);
  background: -moz-linear-gradient(#ffffff, #e4e4e4);
  background: -o-linear-gradient(#ffffff, #e4e4e4);
  background: -ms-linear-gradient(#ffffff, #e4e4e4);
  background: linear-gradient(#ffffff, #e4e4e4);
  border: 1px solid #b2b2b2;
  color: #666666;
  padding: 5px 24px;
  padding-bottom: 3px;
  font-size: 12px;
  cursor: pointer;
  font-family: Helvetica;
  -moz-border-radius: 0px;
  -webkit-border-radius: 0px;
  -o-border-radius: 0px;
  -ms-border-radius: 0px;
  -khtml-border-radius: 0px;
  border-radius: 0px;
  margin-right: -5px;
}
.segmented-button label {
  *display: inline;
}
.segmented-button label:hover {
  -moz-box-shadow: 0 0 5px 1px rgba(0, 0, 0, 0.1);
  -webkit-box-shadow: 0 0 5px 1px rgba(0, 0, 0, 0.1);
  -o-box-shadow: 0 0 5px 1px rgba(0, 0, 0, 0.1);
  box-shadow: 0 0 5px 1px rgba(0, 0, 0, 0.1);
  color: #333333;
}
.segmented-button label:active, .segmented-button label.active {
  background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #e4e4e4), color-stop(100%, #ffffff));
  background: -webkit-linear-gradient(#e4e4e4, #ffffff);
  background: -moz-linear-gradient(#e4e4e4, #ffffff);
  background: -o-linear-gradient(#e4e4e4, #ffffff);
  background: -ms-linear-gradient(#e4e4e4, #ffffff);
  background: linear-gradient(#e4e4e4, #ffffff);
}
.segmented-button label:disabled, .segmented-button label.disabled {
  background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #ffffff), color-stop(100%, #efefef));
  background: -webkit-linear-gradient(#ffffff, #efefef);
  background: -moz-linear-gradient(#ffffff, #efefef);
  background: -o-linear-gradient(#ffffff, #efefef);
  background: -ms-linear-gradient(#ffffff, #efefef);
  background: linear-gradient(#ffffff, #efefef);
  cursor: default;
  color: #b2b2b2;
  border-color: #cccccc;
  -moz-box-shadow: none;
  -webkit-box-shadow: none;
  -o-box-shadow: none;
  box-shadow: none;
}
.segmented-button label.first {
  -moz-border-radius-topleft: 4px;
  -webkit-border-top-left-radius: 4px;
  -o-border-top-left-radius: 4px;
  -ms-border-top-left-radius: 4px;
  -khtml-border-top-left-radius: 4px;
  border-top-left-radius: 4px;
  -moz-border-radius-bottomleft: 4px;
  -webkit-border-bottom-left-radius: 4px;
  -o-border-bottom-left-radius: 4px;
  -ms-border-bottom-left-radius: 4px;
  -khtml-border-bottom-left-radius: 4px;
  border-bottom-left-radius: 4px;
}
.segmented-button label.last {
  -moz-border-radius-topright: 4px;
  -webkit-border-top-right-radius: 4px;
  -o-border-top-right-radius: 4px;
  -ms-border-top-right-radius: 4px;
  -khtml-border-top-right-radius: 4px;
  border-top-right-radius: 4px;
  -moz-border-radius-bottomright: 4px;
  -webkit-border-bottom-right-radius: 4px;
  -o-border-bottom-right-radius: 4px;
  -ms-border-bottom-right-radius: 4px;
  -khtml-border-bottom-right-radius: 4px;
  border-bottom-right-radius: 4px;
}
.segmented-button input:checked + label, .segmented-button label.selected {
  background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #e4e4e4), color-stop(100%, #ffffff));
  background: -webkit-linear-gradient(#e4e4e4, #ffffff);
  background: -moz-linear-gradient(#e4e4e4, #ffffff);
  background: -o-linear-gradient(#e4e4e4, #ffffff);
  background: -ms-linear-gradient(#e4e4e4, #ffffff);
  background: linear-gradient(#e4e4e4, #ffffff);
}
.segmented-button input:disabled + label {
  background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #ffffff), color-stop(100%, #efefef));
  background: -webkit-linear-gradient(#ffffff, #efefef);
  background: -moz-linear-gradient(#ffffff, #efefef);
  background: -o-linear-gradient(#ffffff, #efefef);
  background: -ms-linear-gradient(#ffffff, #efefef);
  background: linear-gradient(#ffffff, #efefef);
  cursor: default;
  color: #b2b2b2;
  border-color: #cccccc;
  -moz-box-shadow: none;
  -webkit-box-shadow: none;
  -o-box-shadow: none;
  box-shadow: none;
}

这是 fiddle http://jsfiddle.net/schinckel/BLkmc/3/light/

最佳答案

不要使用display: none 隐藏输入,当然你不能聚焦不属于页面的元素。尝试另一种方法:

.segmented-button input[type="radio"] {
    position: absolute;
    top: -1000px;
}

http://jsfiddle.net/BLkmc/34/ (结果:http://jsfiddle.net/BLkmc/34/embedded/result/)

关于javascript - CSS 样式的单选按钮不响应键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16373332/

相关文章:

javascript - 尝试在 React 组件中运行 Ko-Fi 小部件。在 DIV 中运行 &lt;script&gt; 命令

javascript - 如何在 Scroll 上为 Div 设置动画

angular - 如何在轮播中为移动(小型)设备使用不同的(小型)图像?

javascript - 监听JS中的事件

javascript - 对于具有局部变量的对象,JSON 字符串化然后解析会意外地工作

php - 邮件功能不起作用....我不明白原因

html - 如何使 div 表现得像溢出 :hidden and overflow:visible

html - CSS响应高度失败

javascript - 带进度条的倒数计时器

javascript - 如何用 DOM 中下一个 img src 替换 div 背景?