简体   繁体   English

使用 JS/jQuery 将 tabindex 附加到输入标签

[英]Appending tabindex to input tag using JS/jQuery

I am trying to set the tabindex for input fields but I don't have access to the HTML.我正在尝试为输入字段设置 tabindex,但我无权访问 HTML。

We are inserting a form that is generated from a third party.我们正在插入一个由第三方生成的表单。 This form has two fields, postal code and email address, in that order.此表单有两个字段,邮政编码和电子邮件地址,按顺序排列。 I want to rearrange them (without having to modify the field on the source platform) using CSS so email is above postal code, but obviously this messes up the tab order.我想使用 CSS 重新排列它们(无需修改源平台上的字段),因此电子邮件高于邮政编码,但显然这会扰乱标签顺序。

The inputs have name attributes of PostalCode and EmailAddress respectively.输入分别具有 PostalCode 和 EmailAddress 的名称属性。 I know very little about JS so am trying to kludge this together from resources on the Internet and so far with no success.我对 JS 知之甚少,因此我试图从 Internet 上的资源中将其拼凑在一起,但迄今为止没有成功。

I have tried the following but it doesn't work and I am obviously unable to figure out why or how to do it properly.我已经尝试了以下但它不起作用,我显然无法弄清楚为什么或如何正确地做到这一点。

document.getElementsByName("EmailAddress")[0].setAttribute("tabIndex", "1");
document.getElementsByName("PostalCode")[0].setAttribute("tabIndex", "2");
document.getElementsByClass("at-submit")[0].setAttribute("tabIndex", "3");

This works.这有效。

Notice I just increment the element to have a higher index.请注意,我只是增加了元素以获得更高的索引。

 const displayTabIndex = (name, { tabIndex }) => console.log(`${name} - ${tabIndex}`); document.addEventListener('DOMContentLoaded', () => { const tel = document.querySelector('input[type="tel"'); const pCode = document.querySelector('input[name="PostalCode"]'); displayTabIndex('tel', tel); displayTabIndex('pCode', pCode); pCode.setAttribute('tabIndex', 3); displayTabIndex('tel', tel); displayTabIndex('pCode', pCode); });
 <form> <input type="tel" name="tel" tabindex="2"> <input type="text" name="PostalCode" tabindex="1"> </form>

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

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