[英]I want to use JavaScript to select an input when I'm tab focused on a button element and press “Enter.”
New dev here trying to get comfortable with JavaScript/jQuery.这里的新开发人员试图熟悉 JavaScript/jQuery。
I'm currently developing a page with tabs that shuffles content on my page based on which tab (button) is selected.我目前正在开发一个带有选项卡的页面,该选项卡根据选择的选项卡(按钮)在我的页面上随机播放内容。 The tabs are based on this code: https://codepen.io/broskibro/pen/VwKRmKQ
选项卡基于以下代码: https://codepen.io/broskibro/pen/VwKRmKQ
<input type="radio" id="tab1" name="tab-control" checked>
<input type="radio" id="tab2" name="tab-control">
<ul>
<li id="list1" title="Job Seeker"><label tabindex="0" class="btn lg" for="tab1" role="button">
<br><span>Job Seeker</span></label></li>
<li id="list2" title="Employer"><label tabindex="0" class="btn lg" for="tab2" role="button">
<br><span>Employer</span></label></li>
</ul>
I've gotten all the functionality that I need from it, but the outstanding issue is that I need the feature to be accessible.我已经从中获得了我需要的所有功能,但突出的问题是我需要该功能是可访问的。 It's easy enough to add
tab-index=0
to the buttons so that they can be reached via tabbing, but the problem is that the input
is what needs to be tab-indexed to and "Enter-ed" to actually activate the functionality.将
tab-index=0
添加到按钮很容易,以便可以通过 tab 键访问它们,但问题是input
需要被 tab 索引和“Enter-ed”才能实际激活功能。 I want to make it so that I can use JS so that when I'm focused on the buttons and I press enter, the respective is activated.我想这样做,以便我可以使用 JS,以便当我专注于按钮并按 enter 时,相应的被激活。 I'm assuming it will be something using the
keydown
feature.我假设这将是使用
keydown
功能的东西。 I'm repurposing some code I got to work for another project I was working on, but I imagine it would look something like:我正在重新利用一些代码,以便为我正在从事的另一个项目工作,但我想它看起来像:
var input = document.getElementById("list1")
input.addEventListener("keydown", function(event) {
if (event.key == 'Enter') {
// Trigger the click for the input
document.getElementById("tab1").click();
}
});
It looks like it should be a relatively easy solution but I'm getting a variety of different errors.看起来它应该是一个相对简单的解决方案,但我遇到了各种不同的错误。 Any guidance is appreciated!
任何指导表示赞赏!
I can't get tabbing to work in the codepen example, so its difficult to test the solution.我无法在 codepen 示例中使用选项卡,因此很难测试解决方案。 But it seems like you are on the right track, I wrote up this:
但看起来你在正确的轨道上,我写了这个:
var input = document.getElementsByTagName("input");
for (var i = 0; i < input.length; i++) {
input[i].addEventListener("keydown", function(event) {
if (event.key == 'Enter') {
// Trigger the click for the input
var clickEvent = new MouseEvent("click", {
"view": window,
"bubbles": true,
"cancelable": false
});
document.getElementById("tab" + i).dispatchEvent(clickEvent);
}
});
}
So it turns out that my major issue was that I was adding IDs to the wrong HTML elements.所以事实证明,我的主要问题是我向错误的 HTML 元素添加了 ID。 After adjusting the HTML, the code below works.
调整 HTML 后,下面的代码工作。 Next, I'll look into properly setting up a loop so that I can reduce the size of my function.
接下来,我将研究如何正确设置循环,以便减小 function 的大小。
<input type="radio" id="tab1" name="tab-control" checked>
<input type="radio" id="tab2" name="tab-control">
<ul>
<li title="Job Seeker"><label id="slide1" tabindex="0" class="btn lg" for="tab1" role="button">
<br><span>Job Seeker</span></label></li>
<li title="Employer"><label id="slide2" tabindex="0" class="btn lg" for="tab2" role="button">
<br><span>Employer</span></label></li>
</ul>
document.addEventListener( 'DOMContentLoaded', function() {
const list1 = document.getElementById("slide1");
const output1 = document.querySelector("#tab1");
const list2 = document.getElementById("slide2");
const output2 = document.querySelector("#tab2");
list1.addEventListener("keydown", function(event) {
if (event.key == 'Enter') {
// Trigger the click for the input
output1.click();
}
});
list2.addEventListener("keydown", function(event) {
if (event.key == 'Enter') {
// Trigger the click for the input
output2.click();
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.