【问题标题】:Make select element truly unfocusable使选择元素真正无法聚焦
【发布时间】:2017-01-09 04:23:18
【问题描述】:

我一直在尝试使选择元素无法聚焦。多个消息来源建议将tabIndex 设置为-1。这是我尝试过的,但失败了。

考虑以下示例:

function addEv(id, event, message)
{
  document.getElementById(id).addEventListener(event, function(){
    document.getElementById('console').innerHTML = message;
  });
}
addEv('select', 'focus', 'select focus');
addEv('select', 'blur', 'select blur');

addEv('span', 'focus', 'span focus');
addEv('span', 'blur', 'span blur');

addEv('select-wrapper', 'focus', 'div focus');
addEv('select-wrapper', 'blur', 'div blur');
#select-wrapper {
  display: inline-block;
  padding: 1px;
  border: 1px solid transparent;
}
#select-wrapper:focus {
  border: 1px solid;
  border-radius: 5px;
  outline: none;
}
<input type="text" value="3 glasses">
<div id="select-wrapper" tabindex="0">
  <span id="span">of</span>
  <select id="select" tabindex="-1">
    <option value="beer">Beer</option>
    <option value="wine" selected>Wine</option>
    <option value="vodka">Vodka</option>
  </select>
</div>
<input type="button" value="drink">
<div id="console"></div><br>

如果您浏览元素,一切都会按预期工作。两个输入元素和 div 元素获得焦点。单击显示“of”的跨度也是如此。但是,如果您单击选择元素,它将获得焦点而不是 div。这意味着错误的焦点事件会被触发,并且选择元素的视觉效果会随着它被聚焦而改变。这在 Firefox 和 Chrome 中是一致的,所以我认为这是故意的。

问题是:如何使选择元素无法聚焦,而使围绕它的 div 成为焦点?当然,我仍然希望能够打开下拉菜单并选择一个选项。

【问题讨论】:

  • tabindex 仅在您在元素之间切换而不是在 click 上时有效
  • css 有一个pointer-events 属性可能会有所帮助
  • @JaromandaX 使用pointer-events: none; 使选择元素无法使用,因为我不能再选择任何东西了。否则,这当然是一个完美的解决方案。
  • 那我一定不明白你想要什么
  • 如果无法聚焦选择,则无法“打开”

标签: javascript html drop-down-menu focus tabindex


【解决方案1】:

编辑:此方法在 Firefox 52 或更早版本中停止工作

我自己找到了一个肮脏的解决方案。如果我忽略了 select 元素仍然会触发焦点和模糊事件的事实,我可以通过在 select 元素获得焦点时执行 select.blur()div.focus() 使其看起来像 div 元素成为焦点。请参阅下面的代码。

注意:如果您要使用我的解决方案,请不要忘记手动实现辅助功能,因为正如@nnnnnn 指出的那样,它们将不再起作用。

function addEv(id, event, message)
{
	document.getElementById(id).addEventListener(event, function(){
    document.getElementById('console').innerHTML += message+"<br>";
  });
}
addEv('select', 'focus', 'select focus');
addEv('select', 'blur', 'select blur');

addEv('span', 'focus', 'span focus');
addEv('span', 'blur', 'span blur');

addEv('select-wrapper', 'focus', 'div focus');
addEv('select-wrapper', 'blur', 'div blur');

document.getElementById('select').addEventListener('focus', function(){
	document.getElementById('select').blur();
  document.getElementById('select-wrapper').focus();
});
#select-wrapper {
  display: inline-block;
  padding: 1px;
  border: 1px solid transparent;
}
#select-wrapper:focus {
  border: 1px solid;
  border-radius: 5px;
  outline: none;
}
<input type="text" value="3 glasses">
<div id="select-wrapper" tabindex="0">
  <span id="span">of</span>
  <select id="select" tabindex="-1">
    <option value="beer">Beer</option>
    <option value="wine" selected>Wine</option>
    <option value="vodka">Vodka</option>
  </select>
</div>
<input type="button" value="drink">
<div id="console"></div><br>

【讨论】:

  • 请注意,此解决方案意味着您的页面将不符合可访问性指南,因为无法通过键盘使用 select 元素。 (请记住,有些用户不能使用鼠标或其他指针设备...)
  • 我知道这一点。在我的现实世界应用程序中,我已经为复选框实现了空格键(我需要以类似的方式重新设计)和用于手动选择的上下键,然后我偶然发现了这个问题(够了吗?)。当我写这个答案时,我只是想展示实际问题的解决方案。但是,这很重要,我会为其他阅读我的答案的人添加通知。
【解决方案2】:

除了 tabindex = -1 之外,您还可以将它添加到 CSS 属性中。

  pointer-events: none;

这对我有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多