简体   繁体   English

使用用户脚本设置tabindex?

[英]Setting tabindex using a userscript?

I work entering data online to a form that I would like to change the tabbing order but have had no luck using Greasemonkey and Scriptish. 我的工作是在线输入数据,使之成为我想要更改制表顺序的表格,但是使用Greasemonkey和Scriptish却没有运气。 I am able to remove tabindexes of -1 to make fields accessible in Scriptish. 我能够删除-1的tabindexes以使字段可以用Scriptish访问。 However, whenever I try to set the tabindex on a field, the field ends up being skipped when tabbing. 但是,每当我尝试在字段上设置tabindex时,在制表时该字段最终都会被跳过。

This works to remove tabindex: 这可以删除tabindex:

var eth = document.getElementById('Ethnicities-111');
if (eth)
eth.removeAttribute('tabindex');

This makes a different text area field inaccessible: 这使得无法访问其他文本区域字段:

var tb = document.getElementById('EybComments');
if (tb)
tb.setAttribute('tabindex' '1');

See Sequential focus navigation and the tabindex attribute . 请参阅顺序焦点导航和tabindex属性 If you alter tab orders keep the following points in mind: 如果更改选项卡顺序,请记住以下几点:

  1. If tabindex is not set, then tab order flows naturally as described in the above spec. 如果未设置tabindex上面的规范所述,制表符顺序自然地流动。
  2. If 2 elements have the same tabindex , the natural order will be used when within that tabindex value (see the demo below). 如果2个元素具有相同的tabindex ,则在该tabindex值内时将使用自然顺序(请参见下面的演示)。
  3. If the tabindex is negative, browsers normally won't allow that node to be tabbed to, but browsers don't have to honor this rule . 如果tabindex为负数,则浏览器通常不允许将该节点制表到该节点, 但是浏览器不必遵循此规则
  4. If tabindex is removed, then the last used tab order may still apply (Firefox, etc.) rather than the natural order. 如果删除了tabindex ,则最后使用的Tab顺序可能仍然适用(Firefox等),而不是自然顺序。
    See the demo below. 请参见下面的演示。

This means that it is not enough to removeAttribute('tabindex') -- especially if it was set to -1! 这意味着removeAttribute('tabindex')是不够的-特别是将其设置为-1时! You must set positive values and the positive values should make sense given the surrounding nodes and the desired taborder. 您必须设置正值,并且给定周围节点和所需的Taborder,正值才有意义。

The following demo shows the effects of various types of tabindex changes . 以下演示演示了各种类型的tabindex 更改的影响
Note especially how the "natural" order changes based on the previous tabindex values before they were cleared. 请特别注意在清除之前,“自然”顺序如何基于先前的tabindex值更改

Press the "Run code snippet" button, below. 按下下方的“运行代码段”按钮。

 $("#startInp").focus (); $("button").click ( function (zEvent) { $("#startInp") .focus (); var targNodes = $("td > label > input"); targNodes.removeProp ("tabindex"); $("#endInp") .prop ("tabindex", "7") .prev ("span").text ('Normal next in tab (ti=7)') ; switch (zEvent.target.id) { case "btnUseNatural": targNodes.prev ("span").text ("tabindex not set"); $("#endInp") .prop ("tabindex", "2") .prev ("span").text ('Normal next in tab (ti=2)') ; break; case "btnUseSequential": targNodes.each ( function () { var jThis = $(this); var tabIdx = jThis.data ("tabidx"); jThis.prop ("tabindex", tabIdx); jThis.prev ("span").text ('tabindex = ' + tabIdx); } ); break; case "btnUseSawtooth": targNodes.each ( function () { var jThis = $(this); var tabIdx = jThis.data ("tabidx") + ""; var newTabIdx = 0; switch (tabIdx) { case "1": newTabIdx = 1; break; case "2": newTabIdx = 3; break; case "3": newTabIdx = 5; break; case "4": newTabIdx = 2; break; case "5": newTabIdx = 4; break; case "6": newTabIdx = 6; break; case "-1": newTabIdx = -1; break; } jThis.prop ("tabindex", newTabIdx); jThis.prev ("span").text ('tabindex = ' + newTabIdx); } ); break; case "btnUseAllOnes": case "btnUseAllEights": var tabIdx = zEvent.target.id == "btnUseAllOnes" ? "1" : "8"; targNodes.prop ("tabindex", tabIdx); targNodes.prev ("span").text ('tabindex = ' + tabIdx); $('input[data-tabidx="-1"]').prop ("tabindex", "-1") .prev ("span").text ("tabindex = -1"); ; break; } } ); 
 div { margin: -2ex auto 2em auto; } input { margin: auto 2em auto 0.5ex; } label > span { width: 14ex; display: inline-block; text-align: right; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <h2>Set tab order to:</h2> <div> <button id="btnUseNatural">Natural</button> <button id="btnUseSequential">Sequential</button> <button id="btnUseSawtooth">Sawtooth</button> <button id="btnUseAllOnes">All Ones</button> <button id="btnUseAllEights">All Eights</button> </div> <p> Tab around the following nodes to see the effect of tabindex changes.<br> <label>Start here (ti=1):<input id="startInp" type="text" tabindex="1"></label> </p> <table> <tr> <td><label><span>tabindex not set</span>:<input data-tabidx="1" type="text"></label></td> <td><label><span>tabindex not set</span>:<input data-tabidx="2" type="text"></label></td> <td><label><span>tabindex not set</span>:<input data-tabidx="3" type="text"></label></td> </tr> <tr> <td><label><span>tabindex = -1</span>:<input data-tabidx="-1" tabindex="-1" type="text"></label></td> <td colspan="2"><=== Will almost always be skipped</td> </tr> <tr> <td><label><span>tabindex not set</span>:<input data-tabidx="4" type="text"></label></td> <td><label><span>tabindex not set</span>:<input data-tabidx="5" type="text"></label></td> <td><label><span>tabindex not set</span>:<input data-tabidx="6" type="text"></label></td> </tr> </table> <p> <label><span>Normal next in tab (ti=2)</span>:<input id="endInp" type="text" tabindex="2"></label> </p> 

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

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