javascript - 我们可以在点击时删除 HTML 元素的边框吗?但保持边界为焦点
问题描述
我正在做一个项目,考虑到 Web 可访问性。
代码:
function removeBorder(){
li=document.getElementById("link");
li.classList.add(".remove")
}
body{
background:#dddddd;
}
p:focus{
border:1px solid red;
}
li{
list-style-type:none;
font-size:1rem
padding:30px;
}
a{
text-decoration:none;
}
a:focus{
border:1px solid red;
border-radius:2px;
}
a:hover{
background:orange;
}
.remove{
border:none;
}
<html>
<head>Borders
</head>
<body>
<p tabindex="0">
Click one the page. Then use TAB to navigate the list items</p>
<ul aria-role="list">
<li aria-role="listitem"><a id="link" onclick="removeBorder()" tabindex="0" href="#">Item One</a></li>
<li aria-role="listitem"><a tabindex="0" onclick="removeBorder()" href="#">Item Two</a></li>
<li aria-role="listitem"><a tabindex="0" onclick="removeBorder()" href="#">Item Three</a></li>
</ul>
</body>
</html>
我有两组用户。
1.普通用户
当我将鼠标悬停在元素上时,在这种情况下,“li”我看到了橙色的背景颜色。
问题:当我点击元素时,有一个红色边框。
有没有一种方法可以让我们只在使用选项卡而不是在单击时设置边框?单击时如何删除边框?
2.仅限键盘用户
当我们用标签聚焦时没问题,边框按预期显示为红色。
解决方案
function removeBorder(){
li=document.getElementById("link");
li.classList.add(".remove")
}
body{
background:#dddddd;
}
p:focus{
border:1px solid red;
}
li{
list-style-type:none;
font-size:1rem
padding:30px;
}
a{
text-decoration:none;
}
a:focus:hover{
border:1px solid red;
border-radius:2px;
}
a:hover{
background:orange;
}
.remove{
border:none;
}
<html>
<head>Borders
</head>
<body>
<p tabindex="0">
Click one the page. Then use TAB to navigate the list items</p>
<ul aria-role="list">
<li aria-role="listitem"><a id="link" onclick="removeBorder()" tabindex="0" href="#">Item One</a></li>
<li aria-role="listitem"><a tabindex="0" onclick="removeBorder()" href="#">Item Two</a></li>
<li aria-role="listitem"><a tabindex="0" onclick="removeBorder()" href="#">Item Three</a></li>
</ul>
</body>
</html>
您应该同时添加焦点和悬停,它可以工作。
推荐阅读
- javascript - 当应用程序再次恢复时,FileSystem.uploadAsync 的 promise 会发生什么?
- c# - 时间:2019-05-10 标签:c#merge 2 solution and publish program
- powerquery - 为传入数据准备查询
- logging - 如何将一个模块的输出仅重定向到文件?
- vb.net - 从文本文件更改信息在列表框中的显示方式?
- jquery - Datatable Jquery 显示错误,通知请求未安全发出,使用 HTTPS
- node.js - 如何使用 Google Cloud Functions 更新数据库值
- delphi - 对于 IdTCPServer,引发什么异常以进行清理是否重要?
- midi - music21:从平面乐谱中获取 midi 声音的声音/程序/乐器?
- javascript - 不和谐机器人发送的图像不会加载