简体   繁体   English

如何不让可访问性集中在复选框上

[英]How to not let accessibility focus on checkbox

We have a toggle switch implemented on top of checkbox. 我们在复选框的顶部实现了一个切换开关。 Checkbox is for keeping track of whether toggle switch is on or off. 复选框用于跟踪切换开关是打开还是关闭。 Everything works fine until you turn on accessibility. 一切正常,直到您打开辅助功能。 When accessibility is turned on & user taps on the toggle switch, the focus goes to the checkbox but it's not clickable. 当打开辅助功能并且用户点击切换开关时,焦点将转到复选框,但它不可点击。 However, if user clicks on the label, it's clickable. 但是,如果用户点击标签,则可以点击。 I need to somehow don't let the accessibility focus come to checkbox, but still announce whether it's checked or unchecked on click of toggle switch or the label. 我需要以某种方式不让可访问性焦点出现在复选框中,但仍然会在点击切换开关或标签时宣布它是否已选中或未选中。

I have tried using tabindex & z-index to not let focus come to the checkbox but both are not making any difference. 我已经尝试使用tabindex和z-index来不让焦点进入复选框,但两者都没有任何区别。 I tried using visibility:hidden. 我尝试使用visibility:hidden。 It works, but then accessibility stops announcing if checkbox is checked or not. 它可以工作,但是如果选中复选框,则可访问性会停止通知。

Our implementation : 我们的实施:

<div class="myToggleSwitch >
    ::before
    <inputtype="checkbox" disabled="disabled">
        <label>Label for the Toggle Switch
            <div class="switch" >
                ::before
                <div class="switch-control">
                </div>
                ::after
            </div>
       </label>
       ::after
</div>


You can use CSS to set some styles on the switch when the checkbox is focused. 当关注复选框时,您可以使用CSS在开关上设置一些样式。

For example: 例如:

.myToggleSwitch input[type="checkbox"]:focus + label .switch {
  // Switch styles go here.
  outline: 1px solid blue;
}

In plain English, it reads 'Find any elements with a class of "myToggleSwitch" with a focused checkbox input. 用简单的英语,它会显示“使用聚焦复选框输入查找具有”myToggleSwitch“类的任何元素。 Then find an adjacent label with a child element with a class of "switch" and give it a blue outline.' 然后找到一个带有子元素的相邻标签,其中包含一个“switch”类,并给它一个蓝色轮廓。

And to make sure the checkbox isn't visible on the page you can do the following: 要确保页面上不显示该复选框,您可以执行以下操作:

.myToggleSwitch input[type="checkbox"] {
  position: absolute:
  left: -1000%;
  visibility: hidden;
}

That hides it and positions it off-screen so that it's still focus-able but not visible on the page. 隐藏它并将其定位在屏幕外,这样它仍然可以聚焦但在页面上看不到。

Your HTML structure seems broken. 您的HTML结构似乎已损坏。

There is no relation between the input and it's label. 输入和它的标签之间没有关系。 Neither is the input nested inside the label, nor do you use a for attribute to link both. 输入不是嵌套在标签内,也不是使用for属性来链接它们。

The checkbox is disabled in your example code. 在示例代码中disabled该复选框。

In that case I'm assuming you used some styling that actually does not depend on the checkbox's state? 在那种情况下,我假设您使用了一些实际上不依赖于复选框状态的样式? Because clicking on the label will not change the input's value in your example. 因为单击标签不会更改示例中输入的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM