[英]with keyboard tab key tabindex element is not showing
Hi i want to show edit icon with tab key press
(keyboard accessibility) and once enter key is pressed from keyboard alert must come.嗨,我想显示带有
tab key press
(键盘可访问性)的编辑图标,一旦从键盘中按下 Enter 键,必须发出警报。
STILL I'M LOOKING FOR AN ANSWER !!我仍然在寻找答案!
expected solution (can be either of below one):预期的解决方案(可以是以下之一):
1. best: on tab button press, directly highlight the edit icon on list and on enter key press alert must come
- 1 step 1. 最好:按选项卡按钮,直接突出显示列表中的编辑图标,然后按回车键
alert must come
- 1 步
2. Ok: on tab button press, highlight list item then on next tab press highlight the edit icon and on enter key press alert must come
- 2 step 2. 好的:按选项卡按钮,突出显示列表项,然后在下一个选项卡上按突出显示编辑图标,然后按回车键
alert must come
- 2 步
Note: i tried best solution approach but did not work注意:我尝试了最佳解决方案,但没有奏效
Below image shows non working demo下图显示了非工作演示
Below is what i have tried:以下是我尝试过的:
function editClicked(){ alert('tab and enter key pressed') }
ul{ list-style:none; margin:0; padding:0; } li{ width:100%; height:80px; display:flex; justify-content:space-between; margin-top:20px; } li{ border:1px solid yellow; } li:hover{ border:1px solid red; } li span.edit{ visibility:hidden; } ul li:hover span.edit{ visibility:visible; }
<ul> <li tabindex="0"> <span>content</span> <span onclick="editClicked()" class="edit" tabindex="0" ><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li tabindex="0"> <span>content</span> <span onclick="editClicked()" class="edit" tabindex="0"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li tabindex="0"> <span>content</span> <span onclick="editClicked()" class="edit" tabindex="0"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <ul>
Please help me thanks in advance !!请帮助我提前致谢!!
Having visibility: hidden;
具有
visibility: hidden;
really messes with your ability to focus those items.真的会影响你专注于这些项目的能力。 Even after adding a focus event to the children, it still won't focus on the kids.
即使在为孩子添加焦点事件后,它仍然不会将焦点放在孩子身上。 So i've changed the
visibility
to visible
.所以我已将
visibility
更改为visible
。 Maybe someone can make it work with hidden
, but i don't know how.也许有人可以让它与
hidden
工作,但我不知道如何。
Fixing your tabindexes
and adding a keydown
event to the document made the 2nd expected solution possible.修复您的
tabindexes
并向文档添加keydown
事件使第二个预期的解决方案成为可能。
function editClicked(){ alert('editClicked clicked') } document.addEventListener('keydown', logKey); function logKey(e) { if (e.code != "Enter") return; let a = document.activeElement; // active element if (a.className == "edit") { editClicked(); } }
ul{ list-style:none; margin:0; padding:0; } li{ width:100%; height:80px; display:flex; justify-content:space-between; margin-top:20px; } li{ border:1px solid yellow; } li:focus{ border:1px solid red; } li .edit{ visibility:visible; } ul li:focus .edit, ul li .edit:focus { visibility:visible; }
<ul> <li tabindex="1"> <span>content</span> <span tabindex="2" onclick="editClicked()" class="edit"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li tabindex="3"> <span>content</span> <span tabindex="4" onclick="editClicked()" class="edit"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li tabindex="5"> <span>content</span> <span tabindex="6" onclick="editClicked()" class="edit"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <ul>
Solution解决方案
li .edit{
opacity: 0;
}
ul li:focus .edit, ul li .edit:focus {
opacity: 1;
}
function editClicked(){ alert('editClicked clicked') } document.addEventListener('keydown', logKey); function logKey(e) { if (e.code != "Enter") return; let a = document.activeElement; // active element if (a.className == "edit") { editClicked(); } }
ul{ list-style:none; margin:0; padding:0; } li{ width:100%; height:80px; display:flex; justify-content:space-between; margin-top:20px; } li{ border:1px solid yellow; } li:focus{ border:1px solid red; } li .edit{ opacity: 0; } ul li:focus .edit, ul li .edit:focus { opacity: 1; }
<ul> <li> <span>content</span> <span tabindex="1" onclick="editClicked()" class="edit"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li> <span>content</span> <span tabindex="2" onclick="editClicked()" class="edit"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li> <span>content</span> <span tabindex="3" onclick="editClicked()" class="edit"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <ul>
Try this:尝试这个:
Option 1: fill color!选项1:填充颜色!
function editClicked(){ alert('tab and enter key pressed') } function handleEnter(e){ var keycode = (e.keyCode ? e.keyCode : e.which); if (keycode == '13') { document.activeElement.click(); } }
ul{ list-style:none; margin:0; padding:0; } li{ width:100%; height:80px; display:flex; justify-content:space-between; margin-top:20px; } li{ border:1px solid yellow; background-color:#fff; } li:hover{ border:1px solid red; } li span svg.editc{ fill: #fff; } ul li:hover span svg.editc{ fill: black; } .edit:focus .editc{ fill: black; } .edit{ height: 1em; }
<body onkeypress="handleEnter(event)"> <ul> <li> <span>content</span> <span onclick="editClicked()" class="edit" tabindex="0" ><svg class="editc" width="1em" height="1em" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li> <span>content</span> <span onclick="editClicked()" class="edit" tabindex="0"><svg width="1em" height="1em" viewBox="0 0 16 16" class="editc" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li> <span>content</span> <span onclick="editClicked()" class="edit" tabindex="0"><svg width="1em" height="1em" viewBox="0 0 16 16" class="editc" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <ul>
Option 2: Opacity!选项 2:不透明!
function editClicked(){ alert('tab and enter key pressed') } function handleEnter(e){ var keycode = (e.keyCode ? e.keyCode : e.which); if (keycode == '13') { document.activeElement.click(); } }
ul{ list-style:none; margin:0; padding:0; } li{ width:100%; height:80px; display:flex; justify-content:space-between; margin-top:20px; } li{ border:1px solid yellow; background-color:#fff; } li:hover{ border:1px solid red; } li span svg.editc{ opacity: 0; } ul li:hover span svg.editc{ opacity: 1; } .edit:focus .editc{ opacity: 1; } .edit{ height: 1em; }
<body onkeypress="handleEnter(event)"> <ul> <li> <span>content</span> <span onclick="editClicked()" class="edit" tabindex="0" ><svg class="editc" width="1em" height="1em" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li> <span>content</span> <span onclick="editClicked()" class="edit" tabindex="0"><svg width="1em" height="1em" viewBox="0 0 16 16" class="editc" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li> <span>content</span> <span onclick="editClicked()" class="edit" tabindex="0"><svg width="1em" height="1em" viewBox="0 0 16 16" class="editc" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <ul>
UPDATE: fix tabindex to 0;更新:修复 tabindex 为 0;
Hope i understand your question correct.希望我理解你的问题是正确的。 I removed
tabindex
from spans and added notes inside code.我从跨度中删除了
tabindex
并在代码中添加了注释。
function editClicked(){ alert('tab and enter key pressed') } /***********************/ // iterate list items document.querySelectorAll('li').forEach(element => { // on focus event: element.addEventListener('focus', function() { // color borders this.classList.add('liHover'); // show current pencil this.querySelectorAll('.edit')[0].classList.add('editHover'); // listen to keyboard this.addEventListener('keypress',editThis); }); // on blur event - do the opposite. element.addEventListener('blur', function() { this.classList.remove('liHover'); this.querySelectorAll('.edit')[0].classList.remove('editHover'); this.removeEventListener('keypress',editThis); }); }); // this will activate when list item is on focus function editThis(e) { if (e.key === 'Enter') { editClicked(); } }
ul{ list-style:none; margin:0; padding:0; } li{ width:100%; height:80px; display:flex; justify-content:space-between; margin-top:20px; } li{ border:1px solid yellow; } .liHover, li:hover{ border:1px solid red; } li span.edit{ visibility:hidden; } .editHover, ul li:hover span.edit{ visibility:visible!important; }
<ul> <li tabindex="0"> <span>content</span> <span onclick="editClicked()" class="edit"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li tabindex="0"> <span>content</span> <span onclick="editClicked()" class="edit"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <li tabindex="0"> <span>content</span> <span onclick="editClicked()" class="edit"><svg width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="bi bi-pen-fill"><path fill-rule="evenodd" d="M13.498.795l.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z"></path></svg></span> </li> <ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.